How should I specify HTTP accept header for multipart response? - http

I want to communicate to a HTTP endpoint that for its multipart response, I want part 1 (or reference a content-disposition filename) to be in JSON and part 2 to be in XML, what's right way to do that?
I can include both JSON and XML but then that alone doesn't communicate my intention of wanting different formats for each part.
EDIT:
Suppose I have a service and right now it's returning something along the line of:
Content-Type: multipart/mixed; boundary=--37adc569155a4943b203e28a422cb96f
Content-Length: ...
----37adc569155a4943b203e28a422cb96f
Content-Type: application/xml; charset=utf-8
Content-Disposition: result
<Result>
...
</Result>
----37adc569155a4943b203e28a422cb96f
Content-Type: application/json; charset=utf-8
Content-Disposition: state
{ "Score": 42, ... }
----37adc569155a4943b203e28a422cb96f--
I can, and want to support passing the data back in different formats, for instance, using protocol buffer for sending the state back or using JSON for result.
I figured the right way to do it would be via the HTTP Accept header, but how do I communicate to the service that I want the state in JSON and result in protocol buffer? If the Accept header is not the way to go, what should I be using instead?

I think what you are looking to do is not defined by any of the RFCs and calls for using custom header fields. I would add headers like
X-Foo-Accept-State: application/json; charset=utf-8
X-Foo-Accept-Result: application/xml; charset=utf-8
(Where "Foo" is your company name. See elsewhere for arguments about the best way to name custom header fields.)

Related

Why is the `Content-Encoding` header named that way?

Background
A server can send a Content-Encoding header to indicate if, and how, the content of the response body has been compressed. E.g.
Content-Encoding: gzip
A server can also send a Content-Type header to indicate the media type, and optionally provide the standard used to encode the content. E.g.
Content-Type: text/html; charset=utf-8
Therefore, it seems that the encoding of the content is specified in the Content-Type header, and not the Content-Encoding header.
Question
During the design of the HTTP standard, what's the rationale behind the naming of the Content-Encoding header? (Over, say Content-Compression) Is this a similar case of bad naming like the 401 Unauthorized response code?

Make POST request with file and xml content

I am trying to make a POST request to an api server.
I have a request body which is an xml parameter:
<create-user>
<user-name>username1</user-name>
<password>password1</password>
</create-user>
Next to this parameter, i also need so send a file.
I've tried with fiddler the following thing, but the data is not received by the server (the file exists, but the <create-user> parameter not.
Is possible to send a combination of xml parameters with uploaded files?
Yes. Just add boundary mark (and "Content-Type: application/xml") before XML body. Like the following:
------yxz
Content-Disposition: form-data; name="formInputXML"
Content-Type: application/xml
<create-user>...</create-user
------xyz
Content-Disposition: form-data; name="formInputFile"; filename="UserData.xml"
Content-Type: text/xml
<UserData.xml content>
------xyz--

How to get file type of HTTP response?

Is there a way to get the file type(extension) of http response? It is not doable to parse http request because some times there will not be file name in the url, for example, "GET www.stackoverflow.com"
HTTP isn't concerned about file types or file extensions, but uses MIME types to distinguish between different content types. As mentioned by shyam, it is represented by the Content-Type header, which for normal web pages may look like this:
Content-Type: text/html; charset=utf-8
An exception is when the HTTP response is serving a file which is supposed to be stored on the client side, in which case a Content-Disposition header may be included to indicate a filename and thus a file extension:
Content-Disposition: attachment; filename="fname.ext"
You can try to guess from the Content-Type header

RESTfully request sub-parts of a representation be of a certain content-type

I am requesting (via Accept: application/json) that an API I'm designing respond as JSON. However, I want the values within that JSON to be specified to conform to text/plain or text/html depending on the capabilities of the client.
What is the RESTful best practice for a "sub-type"? How would this work if I formally switched to HAL as the top-level container?
Accept: application/json+text/plain
{
"value": "Hello World"
}
Accept: application/json+text/html
{
"value": "<h2>Hello World</h2>"
}
Have you considered using a parameter in your Accept: header? See for example how the profile parameter is defined for JSON-LD http://json-ld.org/spec/latest/json-ld-syntax/#application-ld-json. So for example your accept header would be
application/json; profile="http://mysite.org/json-type/html"
Also bear in mind that X- fields as defined in the RFC 1341 are deprecated: https://www.rfc-editor.org/rfc/rfc6648

Why .Net WebApi don't detect the request contentType automatically and do auto-binding?

Why .Net WebApi don't detect the request contentType automatically and do auto-binding?
If I make a request without informing the contentType a HTTP 500 error occour:
No MediaTypeFormatter is available to read an object of type 'ExampleObject' from content with media type ''undefined''.
why not try to detect the incoming data and bind automatically?
Another case:
This request with Content-Type: application/x-www-form-urlencoded send a JSON:
User-Agent: Fiddler
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Host: localhost:10329
Content-Length: 42
Request Body:
{"Name":"qq","Email":"ww","Message":"ee"}:
My Action don't detect the JSON request data automatically in object param:
public void Create(ExampleObject example) //example is null
{
{
Instead of letting the object null why they do not try to solve it?
Then, for the binding occurs I need to send with Content-Type: application/json.
It would be best if .Net WebAPI detects the type of request data and do a auto-binding? Why not in this way?
application/x-www-form-urlencoded means you will be sending data in the x-www-form-urlencoded standard. Sending data in another standard will not work.
Sounds like what you want to do is accept multiple formats from the server.
the way http works is that the client makes a request to the server for a resource and tells the server what content types it understands. This means that the client doesnt get a response it isnt able to decode, and the server knows which responses are more appropriate on the client. For example if you are a web-browser the most appropriate content type is text/html but if you get XML you can probably do something with that too. So you would make a request with the following:
accept: text/html, application/xml
this says you prefer html but also understand XML
In your example if your client wants application/x-www-form-urlencoded but can also deal with JSON then you should do the following when making a request
accept: application/x-www-form-urlencoded, application/json
For more details see the HTTP Spec on accept headers here http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
You may also want to create a new media type formatter so your server knows how to give clients application/x-www-form-urlencoded, take a look at this blog post for more info on how to do this http://www.strathweb.com/2012/04/rss-atom-mediatypeformatter-for-asp-net-webapi/

Resources