hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Carl-Gustaf Harroch <charr...@gmail.com>
Subject Re: best practice for name/value parameter in httprequest
Date Tue, 16 Mar 2010 00:18:05 GMT
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 <charroch@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 <olegk@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


Mime
View raw message