You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Carl-Gustaf Harroch <ch...@gmail.com> on 2010/03/15 10:07:06 UTC

best practice for name/value parameter in httprequest

Hello

I have been playing with HttpClient for a while now and I get to know
the lib. I hope I can hit on some of this mailing list knowledge to
find a best practise/pattern regarding sending parameters via a
request.

In 90% of my projects, I will use HttpGet or HttpPost. The 2 have
different ways of implementing parameters (not http params but actual
form/request parameters). For HttpGet, I would use the URIUtils to
generate a URI while with HttpPost, I would use a list of name value
pairs which is set in the entity afterwards.

Now, I am building a very generic request signer which will sign all
HttpRequest ala OAuth. I use a HttpRequestIntereceptor but need to
check the instance of the request before adding the signature. Isn't
there a better way to get all parameters of a request? Should this not
be made generic? Is there any utilty class I could use.

For instance if I have the following 2 requests:
HttpGet get = new HttpGet("http://somestuff.com/?p=v&p2=v2");

HttpPost post = new HttpPost("http://somestuff.com");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair(LOGIN, username));
formparams.add(new BasicNameValuePair(PASSWORD, password));
post.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));

now, I would like a method of the form:

List<NameValuePair> getNameValueParams(HttpRequest request);
or have the method against HttpRequest.

The above is quite easily implementable but found it so basic and
general that there must be somebody else hitting similar wonders. Any
recommendations?

Cheers,
./C

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: best practice for name/value parameter in httprequest

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2010-03-16 at 00:18 +0000, Carl-Gustaf Harroch wrote:
> To give a more palpable example, lets say I want to add a NameValue
> pair to a GET request. I have the following:
> 
> HttpGet get = new HttpGet("http://google.com/search");
> ...
> List<NameValuePair> l = URLEncodedUtils.parse(get.getURI(), "UTF-8");
> l.add(new NameValuePair("q", "test");
> URI uri = URIUtils.createURI("http", "google.com", -1, "/search",
>     URLEncodedUtils.format(l, "UTF-8"), null);
> get.setURI(uri);
> 
> This is quite similar to POST and I just feel it to be a bit verbose.
> Any best practices lying around?
> 

Carl-Gustaf,

HttpClient is an HTTP transport library, and as such it should treat
semantically different methods differently. Even if from the standpoint
of the RESTful programming model the distinction between body parameters
and query parameters is not important, it does not mean this is the case
for other use cases.

Having said that, you are welcome to contribute additional utility
methods to the URLEncodedUtils class, though.

Oleg


> On 16 March 2010 00:06, Carl-Gustaf Harroch <ch...@gmail.com> wrote:
> > Just found it after I posted my query. Still, I believe there is room
> > for improvements. Maybe a wrapper which would add the ability to
> > add/delete/get/clear a NameValuePair from the Entity/URI against a
> > POST/PUT/GET etc... Throughout development, defining and setting
> > parameters is probably what I do most - creating the URI/Entity during
> > request creation. Especially in REST calls. For instance adding a
> > session token - in the entity or URI depending on the request - is
> > quite common and in terms of concern should not be on the HttpRequest
> > in my opinion. I am currently implementing a HttpRequestInterceptor
> > for the forth mentioned problem.
> >
> > Thanks for the input,
> > ./C
> >
> > On 15 March 2010 22:09, Oleg Kalnichevski <ol...@apache.org> wrote:
> >> Carl-Gustaf Harroch wrote:
> >>>
> >>> Hello
> >>>
> >>> I have been playing with HttpClient for a while now and I get to know
> >>> the lib. I hope I can hit on some of this mailing list knowledge to
> >>> find a best practise/pattern regarding sending parameters via a
> >>> request.
> >>>
> >>> In 90% of my projects, I will use HttpGet or HttpPost. The 2 have
> >>> different ways of implementing parameters (not http params but actual
> >>> form/request parameters). For HttpGet, I would use the URIUtils to
> >>> generate a URI while with HttpPost, I would use a list of name value
> >>> pairs which is set in the entity afterwards.
> >>>
> >>> Now, I am building a very generic request signer which will sign all
> >>> HttpRequest ala OAuth. I use a HttpRequestIntereceptor but need to
> >>> check the instance of the request before adding the signature. Isn't
> >>> there a better way to get all parameters of a request? Should this not
> >>> be made generic? Is there any utilty class I could use.
> >>>
> >>> For instance if I have the following 2 requests:
> >>> HttpGet get = new HttpGet("http://somestuff.com/?p=v&p2=v2");
> >>>
> >>> HttpPost post = new HttpPost("http://somestuff.com");
> >>> List<NameValuePair> formparams = new ArrayList<NameValuePair>();
> >>> formparams.add(new BasicNameValuePair(LOGIN, username));
> >>> formparams.add(new BasicNameValuePair(PASSWORD, password));
> >>> post.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
> >>>
> >>> now, I would like a method of the form:
> >>>
> >>> List<NameValuePair> getNameValueParams(HttpRequest request);
> >>> or have the method against HttpRequest.
> >>>
> >>> The above is quite easily implementable but found it so basic and
> >>> general that there must be somebody else hitting similar wonders. Any
> >>> recommendations?
> >>>
> >>
> >> Have you looked at URLEncodedUtils?
> >>
> >> http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
> >>
> >> Oleg
> >>
> >>> Cheers,
> >>> ./C
> >>>
> >>> ---------------------------------------------------------------------
> >>> 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
> >>
> >>
> >
> >
> >
> > --
> > Carl-Gustaf Harroch
> >
> 
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: best practice for name/value parameter in httprequest

Posted by Carl-Gustaf Harroch <ch...@gmail.com>.
To give a more palpable example, lets say I want to add a NameValue
pair to a GET request. I have the following:

HttpGet get = new HttpGet("http://google.com/search");
...
List<NameValuePair> l = URLEncodedUtils.parse(get.getURI(), "UTF-8");
l.add(new NameValuePair("q", "test");
URI uri = URIUtils.createURI("http", "google.com", -1, "/search",
    URLEncodedUtils.format(l, "UTF-8"), null);
get.setURI(uri);

This is quite similar to POST and I just feel it to be a bit verbose.
Any best practices lying around?

On 16 March 2010 00:06, Carl-Gustaf Harroch <ch...@gmail.com> wrote:
> Just found it after I posted my query. Still, I believe there is room
> for improvements. Maybe a wrapper which would add the ability to
> add/delete/get/clear a NameValuePair from the Entity/URI against a
> POST/PUT/GET etc... Throughout development, defining and setting
> parameters is probably what I do most - creating the URI/Entity during
> request creation. Especially in REST calls. For instance adding a
> session token - in the entity or URI depending on the request - is
> quite common and in terms of concern should not be on the HttpRequest
> in my opinion. I am currently implementing a HttpRequestInterceptor
> for the forth mentioned problem.
>
> Thanks for the input,
> ./C
>
> On 15 March 2010 22:09, Oleg Kalnichevski <ol...@apache.org> wrote:
>> Carl-Gustaf Harroch wrote:
>>>
>>> Hello
>>>
>>> I have been playing with HttpClient for a while now and I get to know
>>> the lib. I hope I can hit on some of this mailing list knowledge to
>>> find a best practise/pattern regarding sending parameters via a
>>> request.
>>>
>>> In 90% of my projects, I will use HttpGet or HttpPost. The 2 have
>>> different ways of implementing parameters (not http params but actual
>>> form/request parameters). For HttpGet, I would use the URIUtils to
>>> generate a URI while with HttpPost, I would use a list of name value
>>> pairs which is set in the entity afterwards.
>>>
>>> Now, I am building a very generic request signer which will sign all
>>> HttpRequest ala OAuth. I use a HttpRequestIntereceptor but need to
>>> check the instance of the request before adding the signature. Isn't
>>> there a better way to get all parameters of a request? Should this not
>>> be made generic? Is there any utilty class I could use.
>>>
>>> For instance if I have the following 2 requests:
>>> HttpGet get = new HttpGet("http://somestuff.com/?p=v&p2=v2");
>>>
>>> HttpPost post = new HttpPost("http://somestuff.com");
>>> List<NameValuePair> formparams = new ArrayList<NameValuePair>();
>>> formparams.add(new BasicNameValuePair(LOGIN, username));
>>> formparams.add(new BasicNameValuePair(PASSWORD, password));
>>> post.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
>>>
>>> now, I would like a method of the form:
>>>
>>> List<NameValuePair> getNameValueParams(HttpRequest request);
>>> or have the method against HttpRequest.
>>>
>>> The above is quite easily implementable but found it so basic and
>>> general that there must be somebody else hitting similar wonders. Any
>>> recommendations?
>>>
>>
>> Have you looked at URLEncodedUtils?
>>
>> http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
>>
>> Oleg
>>
>>> Cheers,
>>> ./C
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>>
>
>
>
> --
> Carl-Gustaf Harroch
>



-- 
Carl-Gustaf Harroch

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: best practice for name/value parameter in httprequest

Posted by Carl-Gustaf Harroch <ch...@gmail.com>.
Just found it after I posted my query. Still, I believe there is room
for improvements. Maybe a wrapper which would add the ability to
add/delete/get/clear a NameValuePair from the Entity/URI against a
POST/PUT/GET etc... Throughout development, defining and setting
parameters is probably what I do most - creating the URI/Entity during
request creation. Especially in REST calls. For instance adding a
session token - in the entity or URI depending on the request - is
quite common and in terms of concern should not be on the HttpRequest
in my opinion. I am currently implementing a HttpRequestInterceptor
for the forth mentioned problem.

Thanks for the input,
./C

On 15 March 2010 22:09, Oleg Kalnichevski <ol...@apache.org> wrote:
> Carl-Gustaf Harroch wrote:
>>
>> Hello
>>
>> I have been playing with HttpClient for a while now and I get to know
>> the lib. I hope I can hit on some of this mailing list knowledge to
>> find a best practise/pattern regarding sending parameters via a
>> request.
>>
>> In 90% of my projects, I will use HttpGet or HttpPost. The 2 have
>> different ways of implementing parameters (not http params but actual
>> form/request parameters). For HttpGet, I would use the URIUtils to
>> generate a URI while with HttpPost, I would use a list of name value
>> pairs which is set in the entity afterwards.
>>
>> Now, I am building a very generic request signer which will sign all
>> HttpRequest ala OAuth. I use a HttpRequestIntereceptor but need to
>> check the instance of the request before adding the signature. Isn't
>> there a better way to get all parameters of a request? Should this not
>> be made generic? Is there any utilty class I could use.
>>
>> For instance if I have the following 2 requests:
>> HttpGet get = new HttpGet("http://somestuff.com/?p=v&p2=v2");
>>
>> HttpPost post = new HttpPost("http://somestuff.com");
>> List<NameValuePair> formparams = new ArrayList<NameValuePair>();
>> formparams.add(new BasicNameValuePair(LOGIN, username));
>> formparams.add(new BasicNameValuePair(PASSWORD, password));
>> post.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
>>
>> now, I would like a method of the form:
>>
>> List<NameValuePair> getNameValueParams(HttpRequest request);
>> or have the method against HttpRequest.
>>
>> The above is quite easily implementable but found it so basic and
>> general that there must be somebody else hitting similar wonders. Any
>> recommendations?
>>
>
> Have you looked at URLEncodedUtils?
>
> http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
>
> Oleg
>
>> Cheers,
>> ./C
>>
>> ---------------------------------------------------------------------
>> 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
>
>



-- 
Carl-Gustaf Harroch

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: best practice for name/value parameter in httprequest

Posted by Oleg Kalnichevski <ol...@apache.org>.
Carl-Gustaf Harroch wrote:
> Hello
> 
> I have been playing with HttpClient for a while now and I get to know
> the lib. I hope I can hit on some of this mailing list knowledge to
> find a best practise/pattern regarding sending parameters via a
> request.
> 
> In 90% of my projects, I will use HttpGet or HttpPost. The 2 have
> different ways of implementing parameters (not http params but actual
> form/request parameters). For HttpGet, I would use the URIUtils to
> generate a URI while with HttpPost, I would use a list of name value
> pairs which is set in the entity afterwards.
> 
> Now, I am building a very generic request signer which will sign all
> HttpRequest ala OAuth. I use a HttpRequestIntereceptor but need to
> check the instance of the request before adding the signature. Isn't
> there a better way to get all parameters of a request? Should this not
> be made generic? Is there any utilty class I could use.
> 
> For instance if I have the following 2 requests:
> HttpGet get = new HttpGet("http://somestuff.com/?p=v&p2=v2");
> 
> HttpPost post = new HttpPost("http://somestuff.com");
> List<NameValuePair> formparams = new ArrayList<NameValuePair>();
> formparams.add(new BasicNameValuePair(LOGIN, username));
> formparams.add(new BasicNameValuePair(PASSWORD, password));
> post.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
> 
> now, I would like a method of the form:
> 
> List<NameValuePair> getNameValueParams(HttpRequest request);
> or have the method against HttpRequest.
> 
> The above is quite easily implementable but found it so basic and
> general that there must be somebody else hitting similar wonders. Any
> recommendations?
> 

Have you looked at URLEncodedUtils?

http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

Oleg

> Cheers,
> ./C
> 
> ---------------------------------------------------------------------
> 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