PostMethod setRequestBody(String) deprecated - why?
Asked Answered
O

2

34

I am using Apache Commons HttpClient PostMethod 3.1.

In the PostMethod class there are also three methods for setting POST method's request body:

setRequestBody(InputStream body)
setRequestBody(String body)
setRequestBody(NameValuePair[] parametersBody);

NameValuePair API

First two methods are deprecated. Does anybody knows why? Because if I want to put an XML to request body, NameValuePair does not help me.

Does anybody knows an workaround or a solution?

Otocyst answered 19/1, 2010 at 9:38 Comment(0)
S
49

The javadoc says:

Deprecated. use setRequestEntity(RequestEntity)

RequestEntity has a lot of implementors, namely:

ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity, MultipartRequestEntity, StringRequestEntity

Use the one that suits you:

and so on.

Salutation answered 19/1, 2010 at 9:43 Comment(1)
StringRequestEntity not found in Suggestion.Standoff
R
5

Yes, so for example,

post.setRequestEntity( new StringRequestEntity( xml ) );

instead of

post.setRequestBody( xml );
Rajiv answered 7/3, 2011 at 0:29 Comment(1)
Unfortunately the constructor StringRequestEntity(String) is deprecated now too. Instead you'll have to use: post.setRequestEntity(new StringRequestEntity(xml, "text/xml", "ISO-8859-1"));Spacing

© 2022 - 2024 — McMap. All rights reserved.