hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mansour Al Akeel <mansour.alak...@gmail.com>
Subject Re: post json in the body with parameters
Date Mon, 21 May 2012 15:00:02 GMT
David,
I appreciate your response.
No, the server doesn't accept multi form data so it's won't work. The
reason I am trying to avoid parameters in the URL is because,
I have a different request that I need to construct with more
parameters. So having something like

if (condition 1)
url = url+"param1=" + val1
if (condition 2)
url = url + "param2" + val 2
else ....
..

It works, but not sure if there's a better way. Plus, with POST we
usually set the param in the body and not in the URL.

I want to use StringEntity to construct the body of the request, but
then I can not set any additional parameters using some thing like:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("commit", "true"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

So I have to set only of them, Either use StringEntity to set the body
of the message, or use UrlEncodedFormEntity.
If I have some complex parameters to be constructed, then I can not
use UrlEncodedFormEntity, and will have to do it
the manual way through appending to the url.

I hope this clarifies the issue.

Thank you.


On Mon, May 21, 2012 at 10:44 AM, David Motes <davidmotes@gmail.com> wrote:
>  The curl example using the url curl
> 'http://localhost:8983/solr/update/json?commit=true'
>  and the HttpClient code using the url String url =
> "http://localhost:8080/solr/update/json?commit=true";
>
>  are doing the same thing..
>
>  Why is it a problem using this url in the HttpClient code?
>  Do you not want to use the StringEntity?
>  Will your server accept multipart post data?
>
>  Some more info will help..
>
> On Mon, May 21, 2012 at 1:19 AM, Mansour Al Akeel
> <mansour.alakeel@gmail.com> wrote:
>> I am trying to post some json data going through this
>> http://wiki.apache.org/solr/UpdateJSON tutorial.
>> As the document shows, the following command should work,
>>
>> curl 'http://localhost:8983/solr/update/json?commit=true'
>> --data-binary @books.json -H 'Content-type:application/json'
>>
>> And it does.
>> However, doing it with HttpClient is a bit different. I need to post
>> JSON data from a string. Including the data requires that I create a
>> StringEntity.
>> However this leaves me with passing any additional parameters, through
>> the URL. So the only way I was able to get it to work, is by adding
>> the parameter (commit=true)
>> to the url as in the following code.
>>
>> private static String url =
>> "http://localhost:8080/solr/update/json?commit=true";
>>        @Override
>>        public void index(ProductData product) {
>>                HttpClient httpclient = new DefaultHttpClient();
>>                HttpPost post = new HttpPost(url);
>>                post.addHeader("Content-type", "application/json");
>>                String d = "[ { \"id\" : \"123\",   \"name\" : \"My Product\"
} ]";
>>                try {
>>                        StringEntity entity = new StringEntity(d);
>>                        entity.setContentEncoding("UTF-8");
>>                        entity.setChunked(true);
>>                        entity.setContentType("application/json");
>>                        post.setEntity(entity);
>>                        HttpResponse response = httpclient.execute(post);
>>                        System.out.println(response);
>>                } catch (UnsupportedEncodingException e) {
>>                        e.printStackTrace();
>>                } catch (ParseException e) {
>>                        e.printStackTrace();
>>                } catch (IOException e) {
>>                        e.printStackTrace();
>>                }
>>        }
>>
>> I googled for this issue, and the closest I was able to find is
>> http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk,
>> where the solution suggests using MultiPartEntity.
>> I am not sure if this is the only way.
>> So my question is, what do I need to do, to be able to able to set the
>> parameters on the post request ??
>>
>> Thank you.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message