hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Motes <davidmo...@gmail.com>
Subject Re: post json in the body with parameters
Date Mon, 21 May 2012 14:44:03 GMT
 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


Mime
View raw message