You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID" <dk...@att.com> on 2012/03/06 00:52:01 UTC

How to pass POST parameters with WebClient.post()?

I'm working with a developer who is trying to call a REST service from his code.  The service can accept a query parameter that could potentially be very long.  It's easy enough to make this call with a GET, but we'd like to avoid the risk of a too-long URL by using a POST instead.  I don't understand how to add POST parameters.

Re: How to pass POST parameters with WebClient.post()?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi,
On 06/03/12 21:59, KARR, DAVID wrote:
>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>> Sent: Tuesday, March 06, 2012 1:53 PM
>> To: users@cxf.apache.org
>> Subject: Re: How to pass POST parameters with WebClient.post()?
>>
>> Hi,
>> On 06/03/12 20:37, KARR, DAVID wrote:
>>>> -----Original Message-----
>>>> From: KARR, DAVID
>>>> Sent: Tuesday, March 06, 2012 12:32 PM
>>>> To: users@cxf.apache.org
>>>> Subject: RE: How to pass POST parameters with WebClient.post()?
>>>>
>>>>> -----Original Message-----
>>>>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>>>>> Sent: Tuesday, March 06, 2012 2:06 AM
>>>>> To: users@cxf.apache.org
>>>>> Subject: Re: How to pass POST parameters with WebClient.post()?
>>>>>
>>>>> Hi
>>>>> On 05/03/12 23:52, KARR, DAVID wrote:
>>>>>> I'm working with a developer who is trying to call a REST service
>>>>> from his code.  The service can accept a query parameter that could
>>>>> potentially be very long.  It's easy enough to make this call with
>> a
>>>>> GET, but we'd like to avoid the risk of a too-long URL by using a
>>>> POST
>>>>> instead.  I don't understand how to add POST parameters.
>>>>>
>>>>> You can get the query parameters added with POST too, but most
>> likely
>>>>> in
>>>>> this case the requirement is to POST the data using the
>>>>> "application/x-www-form-urlencoded" media type.
>>>>> With WebClient you can do it like this:
>>>>>
>>>>> import org.apache.cxf.jaxrs.ext.form.Form;
>>>>>
>>>>> WebClient wc = WebClient.create(address);
>>>>> wc.form(new Form().set("a", aValue).set("b", bValue));
>>>>
>>>> Continuing with this, if the only data we have to send are in the
>> form
>>>> parameters, what would we send in the final "post()" call?  It takes
>> an
>>>> Object.  What is sent there?
>>>
>>> Actually, I guess I figured this out.  We would just pass "null" for
>> that parameter.
>> .form() is the wrapper around the final post(...), so if you do
>> webClient.form(form) then that should be it. MultivaluedMap can also be
>> passed to form(...)
>>
>> Alternatively you can do
>>
>> webClient.type("application/x-www-form-urlencoded");
>> SomeResponse resp = web.post(new Form(), SomeResponse.class);
>
> Is it really necessary to set the MIME type at that point?
webClient.form() sets it internally, post() is more generic so it does 
not check individual objects and try to guess the correct type...
Actually, just checked, I updated the CXF 2.5.3-SNAPSHOT to set the type 
internally if it's Form,

so webClient.type("application/x-www-form-urlencoded");

will be redundant in this case too.


Sergey

RE: How to pass POST parameters with WebClient.post()?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, March 06, 2012 1:53 PM
> To: users@cxf.apache.org
> Subject: Re: How to pass POST parameters with WebClient.post()?
> 
> Hi,
> On 06/03/12 20:37, KARR, DAVID wrote:
> >> -----Original Message-----
> >> From: KARR, DAVID
> >> Sent: Tuesday, March 06, 2012 12:32 PM
> >> To: users@cxf.apache.org
> >> Subject: RE: How to pass POST parameters with WebClient.post()?
> >>
> >>> -----Original Message-----
> >>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> >>> Sent: Tuesday, March 06, 2012 2:06 AM
> >>> To: users@cxf.apache.org
> >>> Subject: Re: How to pass POST parameters with WebClient.post()?
> >>>
> >>> Hi
> >>> On 05/03/12 23:52, KARR, DAVID wrote:
> >>>> I'm working with a developer who is trying to call a REST service
> >>> from his code.  The service can accept a query parameter that could
> >>> potentially be very long.  It's easy enough to make this call with
> a
> >>> GET, but we'd like to avoid the risk of a too-long URL by using a
> >> POST
> >>> instead.  I don't understand how to add POST parameters.
> >>>
> >>> You can get the query parameters added with POST too, but most
> likely
> >>> in
> >>> this case the requirement is to POST the data using the
> >>> "application/x-www-form-urlencoded" media type.
> >>> With WebClient you can do it like this:
> >>>
> >>> import org.apache.cxf.jaxrs.ext.form.Form;
> >>>
> >>> WebClient wc = WebClient.create(address);
> >>> wc.form(new Form().set("a", aValue).set("b", bValue));
> >>
> >> Continuing with this, if the only data we have to send are in the
> form
> >> parameters, what would we send in the final "post()" call?  It takes
> an
> >> Object.  What is sent there?
> >
> > Actually, I guess I figured this out.  We would just pass "null" for
> that parameter.
> .form() is the wrapper around the final post(...), so if you do
> webClient.form(form) then that should be it. MultivaluedMap can also be
> passed to form(...)
> 
> Alternatively you can do
> 
> webClient.type("application/x-www-form-urlencoded");
> SomeResponse resp = web.post(new Form(), SomeResponse.class);

Is it really necessary to set the MIME type at that point?

Re: How to pass POST parameters with WebClient.post()?

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 06/03/12 21:58, KARR, DAVID wrote:
>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>> Sent: Tuesday, March 06, 2012 1:53 PM
>> To: users@cxf.apache.org
>> Subject: Re: How to pass POST parameters with WebClient.post()?
>>
>> Hi,
>> On 06/03/12 20:37, KARR, DAVID wrote:
>>>> -----Original Message-----
>>>> From: KARR, DAVID
>>>> Sent: Tuesday, March 06, 2012 12:32 PM
>>>> To: users@cxf.apache.org
>>>> Subject: RE: How to pass POST parameters with WebClient.post()?
>>>>
>>>>> -----Original Message-----
>>>>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>>>>> Sent: Tuesday, March 06, 2012 2:06 AM
>>>>> To: users@cxf.apache.org
>>>>> Subject: Re: How to pass POST parameters with WebClient.post()?
>>>>>
>>>>> Hi
>>>>> On 05/03/12 23:52, KARR, DAVID wrote:
>>>>>> I'm working with a developer who is trying to call a REST service
>>>>> from his code.  The service can accept a query parameter that could
>>>>> potentially be very long.  It's easy enough to make this call with
>> a
>>>>> GET, but we'd like to avoid the risk of a too-long URL by using a
>>>> POST
>>>>> instead.  I don't understand how to add POST parameters.
>>>>>
>>>>> You can get the query parameters added with POST too, but most
>> likely
>>>>> in
>>>>> this case the requirement is to POST the data using the
>>>>> "application/x-www-form-urlencoded" media type.
>>>>> With WebClient you can do it like this:
>>>>>
>>>>> import org.apache.cxf.jaxrs.ext.form.Form;
>>>>>
>>>>> WebClient wc = WebClient.create(address);
>>>>> wc.form(new Form().set("a", aValue).set("b", bValue));
>>>>
>>>> Continuing with this, if the only data we have to send are in the
>> form
>>>> parameters, what would we send in the final "post()" call?  It takes
>> an
>>>> Object.  What is sent there?
>>>
>>> Actually, I guess I figured this out.  We would just pass "null" for
>> that parameter.
>> .form() is the wrapper around the final post(...), so if you do
>> webClient.form(form) then that should be it. MultivaluedMap can also be
>> passed to form(...)
>>
>> Alternatively you can do
>>
>> webClient.type("application/x-www-form-urlencoded");
>> SomeResponse resp = web.post(new Form(), SomeResponse.class);
>
> And that second parameter could be "JSONObject.class", right (returning a JSONObject parsed from the string, of course)?
Sure, as long as the relevant provider is also registered

Sergey

-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

RE: How to pass POST parameters with WebClient.post()?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, March 06, 2012 1:53 PM
> To: users@cxf.apache.org
> Subject: Re: How to pass POST parameters with WebClient.post()?
> 
> Hi,
> On 06/03/12 20:37, KARR, DAVID wrote:
> >> -----Original Message-----
> >> From: KARR, DAVID
> >> Sent: Tuesday, March 06, 2012 12:32 PM
> >> To: users@cxf.apache.org
> >> Subject: RE: How to pass POST parameters with WebClient.post()?
> >>
> >>> -----Original Message-----
> >>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> >>> Sent: Tuesday, March 06, 2012 2:06 AM
> >>> To: users@cxf.apache.org
> >>> Subject: Re: How to pass POST parameters with WebClient.post()?
> >>>
> >>> Hi
> >>> On 05/03/12 23:52, KARR, DAVID wrote:
> >>>> I'm working with a developer who is trying to call a REST service
> >>> from his code.  The service can accept a query parameter that could
> >>> potentially be very long.  It's easy enough to make this call with
> a
> >>> GET, but we'd like to avoid the risk of a too-long URL by using a
> >> POST
> >>> instead.  I don't understand how to add POST parameters.
> >>>
> >>> You can get the query parameters added with POST too, but most
> likely
> >>> in
> >>> this case the requirement is to POST the data using the
> >>> "application/x-www-form-urlencoded" media type.
> >>> With WebClient you can do it like this:
> >>>
> >>> import org.apache.cxf.jaxrs.ext.form.Form;
> >>>
> >>> WebClient wc = WebClient.create(address);
> >>> wc.form(new Form().set("a", aValue).set("b", bValue));
> >>
> >> Continuing with this, if the only data we have to send are in the
> form
> >> parameters, what would we send in the final "post()" call?  It takes
> an
> >> Object.  What is sent there?
> >
> > Actually, I guess I figured this out.  We would just pass "null" for
> that parameter.
> .form() is the wrapper around the final post(...), so if you do
> webClient.form(form) then that should be it. MultivaluedMap can also be
> passed to form(...)
> 
> Alternatively you can do
> 
> webClient.type("application/x-www-form-urlencoded");
> SomeResponse resp = web.post(new Form(), SomeResponse.class);

And that second parameter could be "JSONObject.class", right (returning a JSONObject parsed from the string, of course)?

Re: How to pass POST parameters with WebClient.post()?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi,
On 06/03/12 20:37, KARR, DAVID wrote:
>> -----Original Message-----
>> From: KARR, DAVID
>> Sent: Tuesday, March 06, 2012 12:32 PM
>> To: users@cxf.apache.org
>> Subject: RE: How to pass POST parameters with WebClient.post()?
>>
>>> -----Original Message-----
>>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>>> Sent: Tuesday, March 06, 2012 2:06 AM
>>> To: users@cxf.apache.org
>>> Subject: Re: How to pass POST parameters with WebClient.post()?
>>>
>>> Hi
>>> On 05/03/12 23:52, KARR, DAVID wrote:
>>>> I'm working with a developer who is trying to call a REST service
>>> from his code.  The service can accept a query parameter that could
>>> potentially be very long.  It's easy enough to make this call with a
>>> GET, but we'd like to avoid the risk of a too-long URL by using a
>> POST
>>> instead.  I don't understand how to add POST parameters.
>>>
>>> You can get the query parameters added with POST too, but most likely
>>> in
>>> this case the requirement is to POST the data using the
>>> "application/x-www-form-urlencoded" media type.
>>> With WebClient you can do it like this:
>>>
>>> import org.apache.cxf.jaxrs.ext.form.Form;
>>>
>>> WebClient wc = WebClient.create(address);
>>> wc.form(new Form().set("a", aValue).set("b", bValue));
>>
>> Continuing with this, if the only data we have to send are in the form
>> parameters, what would we send in the final "post()" call?  It takes an
>> Object.  What is sent there?
>
> Actually, I guess I figured this out.  We would just pass "null" for that parameter.
.form() is the wrapper around the final post(...), so if you do 
webClient.form(form) then that should be it. MultivaluedMap can also be 
passed to form(...)

Alternatively you can do

webClient.type("application/x-www-form-urlencoded");
SomeResponse resp = web.post(new Form(), SomeResponse.class);

Sergey

RE: How to pass POST parameters with WebClient.post()?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: KARR, DAVID
> Sent: Tuesday, March 06, 2012 12:32 PM
> To: users@cxf.apache.org
> Subject: RE: How to pass POST parameters with WebClient.post()?
> 
> > -----Original Message-----
> > From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> > Sent: Tuesday, March 06, 2012 2:06 AM
> > To: users@cxf.apache.org
> > Subject: Re: How to pass POST parameters with WebClient.post()?
> >
> > Hi
> > On 05/03/12 23:52, KARR, DAVID wrote:
> > > I'm working with a developer who is trying to call a REST service
> > from his code.  The service can accept a query parameter that could
> > potentially be very long.  It's easy enough to make this call with a
> > GET, but we'd like to avoid the risk of a too-long URL by using a
> POST
> > instead.  I don't understand how to add POST parameters.
> >
> > You can get the query parameters added with POST too, but most likely
> > in
> > this case the requirement is to POST the data using the
> > "application/x-www-form-urlencoded" media type.
> > With WebClient you can do it like this:
> >
> > import org.apache.cxf.jaxrs.ext.form.Form;
> >
> > WebClient wc = WebClient.create(address);
> > wc.form(new Form().set("a", aValue).set("b", bValue));
> 
> Continuing with this, if the only data we have to send are in the form
> parameters, what would we send in the final "post()" call?  It takes an
> Object.  What is sent there?

Actually, I guess I figured this out.  We would just pass "null" for that parameter.

RE: How to pass POST parameters with WebClient.post()?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, March 06, 2012 2:06 AM
> To: users@cxf.apache.org
> Subject: Re: How to pass POST parameters with WebClient.post()?
> 
> Hi
> On 05/03/12 23:52, KARR, DAVID wrote:
> > I'm working with a developer who is trying to call a REST service
> from his code.  The service can accept a query parameter that could
> potentially be very long.  It's easy enough to make this call with a
> GET, but we'd like to avoid the risk of a too-long URL by using a POST
> instead.  I don't understand how to add POST parameters.
> 
> You can get the query parameters added with POST too, but most likely
> in
> this case the requirement is to POST the data using the
> "application/x-www-form-urlencoded" media type.
> With WebClient you can do it like this:
> 
> import org.apache.cxf.jaxrs.ext.form.Form;
> 
> WebClient wc = WebClient.create(address);
> wc.form(new Form().set("a", aValue).set("b", bValue));

Continuing with this, if the only data we have to send are in the form parameters, what would we send in the final "post()" call?  It takes an Object.  What is sent there?

Re: How to pass POST parameters with WebClient.post()?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 05/03/12 23:52, KARR, DAVID wrote:
> I'm working with a developer who is trying to call a REST service from his code.  The service can accept a query parameter that could potentially be very long.  It's easy enough to make this call with a GET, but we'd like to avoid the risk of a too-long URL by using a POST instead.  I don't understand how to add POST parameters.

You can get the query parameters added with POST too, but most likely in 
this case the requirement is to POST the data using the 
"application/x-www-form-urlencoded" media type.
With WebClient you can do it like this:

import org.apache.cxf.jaxrs.ext.form.Form;

WebClient wc = WebClient.create(address);
wc.form(new Form().set("a", aValue).set("b", bValue));

Cheers, Sergey