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 ?
Thank you. On Mon, May 21, 2012 at 11:18 AM, David Motes <[email protected]> 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 > <[email protected]> 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 <[email protected]> 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 >>> <[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] >>>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe, e-mail: [email protected] >>> For additional commands, e-mail: [email protected] >>> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
