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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Trick
  • 3,779
  • 12
  • 49
  • 76

2 Answers2

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
5

Yes, so for example,

post.setRequestEntity( new StringRequestEntity( xml ) );

instead of

post.setRequestBody( xml );
Tim Stone
  • 19,119
  • 6
  • 56
  • 66
Tony Schwartz
  • 51
  • 1
  • 1
  • 11
    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"));` – Ted Bigham Apr 23 '12 at 21:12