Friday, February 24, 2012

nsurlrequest settings for POST


I am setting up a request to my server, I have been helped out with a few suggestions but I am wanting some clarification on a part of code.



in the second line of code, what are the setValue: and forHTTPHeaderField: values used for? I'm thinking forHTTPHeaderField: sets the mime type... but im not sure what setValue is for or how it effects my request.




[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:postBodyData];



any help would be greatly appreciated.

3 comments:

  1. See the NSMutableURLRequest reference for the method description and HTTP documentation at 14.17 Content-Type section for the header information.

    More C-T details at the section 7.2.1


    Content-Type specifies the media type of the underlying data. Content-Encoding may be used to indicate any additional content codings applied to the data, usually for the purpose of data compression, that are a property of the requested resource. There is no default encoding.

    Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If
    and only if the media type is not given by a Content-Type field, the
    recipient MAY attempt to guess the media type via inspection of its
    content and/or the name extension(s) of the URI used to identify the
    resource. If the media type remains unknown, the recipient SHOULD
    treat it as type "application/octet-stream".

    ReplyDelete
  2. Every HTML request consists of a request header and body.

    In your example you define that the body of this request contains form data.

    If for example you want to submit a json structure as your request body, the content type of the request is to be set as "application/json".

    ReplyDelete
  3. In the case of content-type, it would be things like text/html, text/xml, or image/gif.

    The purpose here is to specify what type of data is being transmitted.

    For header field definitions, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.

    For the different media types, see http://en.wikipedia.org/wiki/Internet_media_type.

    Also see the NSURLRequest reference for specifics of that class.

    ReplyDelete