If it is not possible to set post parameters in a request and set the
body content in the same request,
I will appreciate it if someone can advice me of an alternative.


On Mon, May 21, 2012 at 1:19 AM, Mansour Al Akeel
<[email protected]> 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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to