Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Understanding Content Type in http request and response

Hello can someone please explain me how do you set the type for a request and the type of the response given a html element ?

Example:

If i want to send a form that sends some files and a POCO and receives a stream i would use type=multipart/form-data ; would type in this case refer to the request or the expected response?

<form type="multipart/form-data"><input type="file"></form>

In this case the type refers clearly to the request.But then it means that the server that processes the request has to set the response.type to application/octet-stream.

So following this reason i thought that what you set in the form is the request type.

However then i can not understand this contradiction:

<a class="button" type="application/octet-stream"  href="http://localhost:5300/get" download>Click here for dld</a>

I could not get a file download until i didn't set the type of the response here.I tried setting the response type in the server but it did not work.So in this case here type refers to the response and not the request.

Can someone please shed some light? Is there a list with the html elements on what they refer by type ? (type of the request or the expected response ) ?

PS
Long story short : why does form refer to the request by type and a refers to the response.How can one reason when he wants to send a request whose content is type A and his response is type B.

Upvotes: 1

Views: 1134

Answers (1)

Quentin
Quentin

Reputation: 943100

But then it means that the server that processes the request has to set the response.type to application/octet-stream.

No. The server can respond with whatever type of content it wants to.

application/octet-stream is a pretty meaningless "Here is a stream of bytes". Normally you would want something that describes the actual content type of the response.

Can someone please shed some light?

The type attribute on the <a> element refers to the expected response type. This allows a bot to avoid following the link if it expects to get a response it can't understand.

Since a link triggers a GET request, there is no request body to describe the type of anyway.

Is there a list with the html elements on what they refer by type ?

The HTML spec describes the type attribute for each element it appears on. e.g. script links to this type.


How can one reason when he wants to send a request whose content is type A and his response is type B

You almost never need to tell the client what type to expect, so just don't. Only worry about it when you are constructing HTTP requests with bodies.

Upvotes: 1

Related Questions