415 Unsupported Media Type for XML Post Rest API

2019-07-21 10:48发布

问题:

I am trying to Request for REST POST API with XML string but I am getting following error response.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<apiErrors>    
   <apiError>        
      <errorMessage>415 Unsupported Media Type - null</errorMessage>    
   </apiError>
</apiErrors>

I don't understand why this happening. Even I set content-type and header also.

Please take a look at my code.

String XmlString = "<Contact>"
                                +"<name>Sandeep</name>"
                                +"<title>Mr.</title>"
                                +"<Address>India</Address>"                                 
                                +"</Contact>";      

            try {
                StringEntity se = new StringEntity(XmlString);
                HttpPost httpRequest = new HttpPost("http://abc:xyz123@000.000.000.00:8080/aaa/sample/feed/000001");
                httpRequest.setHeader("Content-Type","application/soap-xml;charset=UTF-8");
                httpRequest.setHeader("Accept","text/plain");

                httpRequest.setEntity(se);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse httpResponse;
                httpResponse = httpclient.execute(httpRequest, new BasicHttpContext());

                finalres = inputStreamToString(httpResponse.getEntity().getContent())
                            .toString();

Please give me hint or reference.

回答1:

Did you try without charset="utf-8"?

(I couldn't leave a comment due to my low reputation)



回答2:

In your request you are specifying the communication to be soap-xml

httpRequest.setHeader("Content-Type","application/soap-xml;charset=UTF-8");

Try to specify to use plain xml by removing the "soap" part.

Valid xml content types:

"text/xml" or "application/xml"

Soap calls are very specifically formatted xml and from the response, unless you trimmed the output to remove all of the soap xml, it appears to be a plain xml service.



回答3:

If you use @Consumes(MediaType.APPLICATION_XML) in your REST service then use content type as follows

httpRequest.setHeader("Content-Type","application/xml;");

Check you application using browser REST clients such as Curl, Advanced rest clent, etc.