hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: post json in the body with parameters
Date Mon, 21 May 2012 15:53:53 GMT
On Mon, 2012-05-21 at 11:32 -0400, Mansour Al Akeel wrote:
> David, I tried this and it didn't work.
> I don't want to modify anything on the server side. This is why I am
> trying to get it to work this way.
> Now I have seen it being done with other frameworks, including httpclient 3.X
> where we had post.setRequestBody(String).
> Does this mean that HttpClient 4.X is missing this ?
> 

Missing exactly what? One can set HTTP entity content and URI request
parameters entirely independently from one another. If you want a
convenience class to help you build HTTP request URIs, feel free to use
URIBuilder from HttpClient 4.2 [1] or a similar utility class. 

Oleg

[1]
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html

> Thank you.
> 
> On Mon, May 21, 2012 at 11:18 AM, David Motes <davidmotes@gmail.com> wrote:
> >  If the server will only accept one body part then you  are stuck..
> >  You can only send the json body part or the form url encoded body part.
> >  You can't mix them.. (I guess you could if you were writing the
> > server software, but that is an aside...)
> >
> >  If the server would accept multi-part you would just create a
> > multi-part entity, add a string entity with the form url encoded part
> > and another string entity with the json part and do the post.
> >
> >  Sorry not much help.
> >
> > On Mon, May 21, 2012 at 11:00 AM, Mansour Al Akeel
> > <mansour.alakeel@gmail.com> wrote:
> >> 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
> >>
> >
> > ---------------------------------------------------------------------
> > 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