YellowPillow
YellowPillow

Reputation: 4290

NSMutableURLRequest addValue method

let request = NSMutableURLRequest(URL: url)
request.addValue("application/json", forHTTPHeaderField: "Accept")

So I have this piece of code and I'm struggling to understand what the addValue method does. I've read online about requests and HTTPHeaderFields but I still can't see to grasp the concept. Would anyone be able to shed some light on this?

Upvotes: 0

Views: 1014

Answers (1)

Rashwan L
Rashwan L

Reputation: 38843

The addValue adds an HTTP header to the receiver’s HTTP header dictionary.

This method provides the ability to add values to header fields incrementally. If a value was previously set for the specified field, the supplied value is appended to the existing value using the appropriate field delimiter. In the case of HTTP, the delimiter is a comma.

Update

HTTP header fields provide required information about the request or response, or about the object sent in the message body

You have added "Accept" in your addValue and the Accept request-header field can be used to specify certain media types which are acceptable for the response.

Reference.

Upvotes: 2

Related Questions