Quantcast

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

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

David M. Karr
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Sergey Beryozkin
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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

David M. Karr
> -----Original Message-----
> From: Sergey Beryozkin [mailto:[hidden email]]
> Sent: Tuesday, March 06, 2012 2:06 AM
> To: [hidden email]
> 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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

David M. Karr
> -----Original Message-----
> From: KARR, DAVID
> Sent: Tuesday, March 06, 2012 12:32 PM
> To: [hidden email]
> Subject: RE: How to pass POST parameters with WebClient.post()?
>
> > -----Original Message-----
> > From: Sergey Beryozkin [mailto:[hidden email]]
> > Sent: Tuesday, March 06, 2012 2:06 AM
> > To: [hidden email]
> > 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Sergey Beryozkin
Hi,
On 06/03/12 20:37, KARR, DAVID wrote:

>> -----Original Message-----
>> From: KARR, DAVID
>> Sent: Tuesday, March 06, 2012 12:32 PM
>> To: [hidden email]
>> Subject: RE: How to pass POST parameters with WebClient.post()?
>>
>>> -----Original Message-----
>>> From: Sergey Beryozkin [mailto:[hidden email]]
>>> Sent: Tuesday, March 06, 2012 2:06 AM
>>> To: [hidden email]
>>> 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

David M. Karr
> -----Original Message-----
> From: Sergey Beryozkin [mailto:[hidden email]]
> Sent: Tuesday, March 06, 2012 1:53 PM
> To: [hidden email]
> 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: [hidden email]
> >> Subject: RE: How to pass POST parameters with WebClient.post()?
> >>
> >>> -----Original Message-----
> >>> From: Sergey Beryozkin [mailto:[hidden email]]
> >>> Sent: Tuesday, March 06, 2012 2:06 AM
> >>> To: [hidden email]
> >>> 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)?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

David M. Karr
In reply to this post by Sergey Beryozkin
> -----Original Message-----
> From: Sergey Beryozkin [mailto:[hidden email]]
> Sent: Tuesday, March 06, 2012 1:53 PM
> To: [hidden email]
> 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: [hidden email]
> >> Subject: RE: How to pass POST parameters with WebClient.post()?
> >>
> >>> -----Original Message-----
> >>> From: Sergey Beryozkin [mailto:[hidden email]]
> >>> Sent: Tuesday, March 06, 2012 2:06 AM
> >>> To: [hidden email]
> >>> 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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Sergey Beryozkin
Hi,
On 06/03/12 21:59, KARR, DAVID wrote:

>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:[hidden email]]
>> Sent: Tuesday, March 06, 2012 1:53 PM
>> To: [hidden email]
>> 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: [hidden email]
>>>> Subject: RE: How to pass POST parameters with WebClient.post()?
>>>>
>>>>> -----Original Message-----
>>>>> From: Sergey Beryozkin [mailto:[hidden email]]
>>>>> Sent: Tuesday, March 06, 2012 2:06 AM
>>>>> To: [hidden email]
>>>>> 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Sergey Beryozkin
In reply to this post by David M. Karr
On 06/03/12 21:58, KARR, DAVID wrote:

>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:[hidden email]]
>> Sent: Tuesday, March 06, 2012 1:53 PM
>> To: [hidden email]
>> 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: [hidden email]
>>>> Subject: RE: How to pass POST parameters with WebClient.post()?
>>>>
>>>>> -----Original Message-----
>>>>> From: Sergey Beryozkin [mailto:[hidden email]]
>>>>> Sent: Tuesday, March 06, 2012 2:06 AM
>>>>> To: [hidden email]
>>>>> 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
Loading...