Return-Path: X-Original-To: apmail-hc-httpclient-users-archive@www.apache.org Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 27021913C for ; Mon, 21 May 2012 15:32:51 +0000 (UTC) Received: (qmail 24434 invoked by uid 500); 21 May 2012 15:32:50 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 24398 invoked by uid 500); 21 May 2012 15:32:50 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 24390 invoked by uid 99); 21 May 2012 15:32:50 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 May 2012 15:32:50 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of mansour.alakeel@gmail.com designates 209.85.215.51 as permitted sender) Received: from [209.85.215.51] (HELO mail-lpp01m010-f51.google.com) (209.85.215.51) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 May 2012 15:32:43 +0000 Received: by lagw12 with SMTP id w12so3473101lag.10 for ; Mon, 21 May 2012 08:32:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=BRMbspmNR4L5I9WE3L3G8xkVI47gSVC+9ECw6/YCr0g=; b=0qBHVr1f2q0VB3mgu+vgQVJ3aLPpCU+bBB/Oi7bMBF6TESHX3MWeSgYgpjUYcVS2NX Y6taDNT5oHLzD17lzXzRqrGHbf3bjGlIfXcp/uq9WJ/el26YA82vnaf34kVolp/sI7h8 ZWROTS7Hx9OmLlJJ36nP5iLc2xvDFs8KwMgO9e2axSrXfLApHXgEAkiv1/yIPGyONDAp JdudBzrQY6YZygBnfF31OdDqiyjbZqDv+vMnpos++wEWFiKOgZ/nAKQ8fWMaQq2+OAhd Gi9qbHCxVzMkHwvd+GWTd6i7A/IcovXm2KD63mKWZ5wo7vAZoK61C0um9Os1H0/kYD0o kuUQ== MIME-Version: 1.0 Received: by 10.152.46.232 with SMTP id y8mr20086316lam.18.1337614342924; Mon, 21 May 2012 08:32:22 -0700 (PDT) Received: by 10.112.63.147 with HTTP; Mon, 21 May 2012 08:32:22 -0700 (PDT) In-Reply-To: References: Date: Mon, 21 May 2012 11:32:22 -0400 Message-ID: Subject: Re: post json in the body with parameters From: Mansour Al Akeel To: HttpClient User Discussion Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 wrote: > =A0If the server will only accept one body part then you =A0are stuck.. > =A0You can only send the json body part or the form url encoded body part= . > =A0You can't mix them.. (I guess you could if you were writing the > server software, but that is an aside...) > > =A0If 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. > > =A0Sorry not much help. > > On Mon, May 21, 2012 at 11:00 AM, Mansour Al Akeel > 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 =3D url+"param1=3D" + val1 >> if (condition 2) >> url =3D 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 nameValuePairs =3D new ArrayList(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 wro= te: >>> =A0The curl example using the url curl >>> 'http://localhost:8983/solr/update/json?commit=3Dtrue' >>> =A0and the HttpClient code using the url String url =3D >>> "http://localhost:8080/solr/update/json?commit=3Dtrue"; >>> >>> =A0are doing the same thing.. >>> >>> =A0Why is it a problem using this url in the HttpClient code? >>> =A0Do you not want to use the StringEntity? >>> =A0Will your server accept multipart post data? >>> >>> =A0Some more info will help.. >>> >>> On Mon, May 21, 2012 at 1:19 AM, Mansour Al Akeel >>> 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=3Dtrue' >>>> --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=3Dtrue) >>>> to the url as in the following code. >>>> >>>> private static String url =3D >>>> "http://localhost:8080/solr/update/json?commit=3Dtrue"; >>>> =A0 =A0 =A0 =A0@Override >>>> =A0 =A0 =A0 =A0public void index(ProductData product) { >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0HttpClient httpclient =3D new DefaultHt= tpClient(); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0HttpPost post =3D new HttpPost(url); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0post.addHeader("Content-type", "applica= tion/json"); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0String d =3D "[ { \"id\" : \"123\", =A0= \"name\" : \"My Product\" } ]"; >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0try { >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0StringEntity entity =3D= new StringEntity(d); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0entity.setContentEncodi= ng("UTF-8"); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0entity.setChunked(true)= ; >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0entity.setContentType("= application/json"); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0post.setEntity(entity); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0HttpResponse response = =3D httpclient.execute(post); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0System.out.println(resp= onse); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} catch (UnsupportedEncodingException e= ) { >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e.printStackTrace(); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} catch (ParseException e) { >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e.printStackTrace(); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} catch (IOException e) { >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e.printStackTrace(); >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} >>>> =A0 =A0 =A0 =A0} >>>> >>>> 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