0

How can I set content type of HTTP Put as xxxx+xml?

I was referring to solution in this link Android, sending XML via HTTP POST (SOAP). Its fine when we set content type like this, i mean the xml is came along with the request:

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

but when i change type soap to something custom, the xml disappear on the request (i saw on the wireshark), like this:

httppost.setHeader("Content-Type","application/vnd.oma-pcc+xml;charset=UTF-8");

then, i tried put the xml only, so the request is ok again:

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

I want to know what exactly the rules for the content-type than come together with the xml type so that the xml still there.

Thanks.

Community
  • 1
  • 1
dejoong
  • 2,213
  • 5
  • 25
  • 24
  • If the MIME type is one of those standardized one, you can use the +xml as you did in the first place. If the you are using custom protocol, you have to supersede it with "vnd" which stands for Vendor. – Abraham Jan 15 '13 at 13:43
  • i've already put vnd.oma-pcc but still not work. anything i missed? – dejoong Jan 15 '13 at 14:03
  • adding "vnd" doesn't make it work unless you have a browser that is able to decode a content of that type. Make sure that you add the necessary library to parse that kind of content. – Abraham Jan 15 '13 at 14:07
  • Umm.., I was execute the request using httpClient.execute(httppost); programmatically using java. Do you have idea how I add the necessary library to parse that content type? – dejoong Jan 15 '13 at 14:22
  • I think this has to do with the server not recognizing this format as the format of its parameter. If you can access the code in the server check if the POST method is annotated with @Consumes("application/vnd.oma-pcc+xml"). You might be able to debug the program and see what exactly the server is responding that might give you the clue. – Abraham Jan 15 '13 at 14:57
  • Are you using Apache HTTP Client? this is not overly well indicated. – Dave G Jan 15 '13 at 14:57
  • @Dave, yes i was using org.apache.http.impl.client.DefaultHttpClient and using the execute(httpPost); – dejoong Jan 16 '13 at 04:28

1 Answers1

1

Assuming you're using HTTPClient of 4.1.3 or greater -

When constructing you're entity, you have the option to specify the content being used for the POST or PUT operation for certain entities.

There is a ContentType object which should be used to specify this.

Using the factory method .create() you can specify the mimetype with a charset - the ContentType will be used by the framework to properly emit the header in question.

Example API call:

ContentType.create("application/vnd.oma-pcc+xml", CharSet.forName("UTF-8"));

NOTE Editing for HttpClient 4.1.2

In the case of 4.1.2, when you create your entity for the post or put operation, set the content type on the entity not the execution (HttpPost or HttpPut) using setContentType(String). This is deprecated in 4.1.3 and beyond.

Dave G
  • 9,639
  • 36
  • 41
  • oh.., unfortune i use version 4.1.2, but i see there is httpmime-4.1.2.jar. now i investigating if it can help me. – dejoong Jan 16 '13 at 05:56
  • @dejoong - I don't believe that httpmime is needed - that API is in the standard httpclient jar – Dave G Jan 16 '13 at 10:35