iOS Developer Library


Foundation Framework Reference NSHTTPURLResponse Class Reference

Deployment Target:

On This Page


The NSHTTPURLResponse class is a subclass of NSURLResponse that provides methods for accessing information specific to HTTP protocol responses. Whenever you make HTTP URL load requests, any response objects you get back from the NSURLSession, NSURLConnection, or NSURLDownload class are instances of the NSHTTPURLResponse class.

  • Initializes an HTTP URL response object with a status code, protocol version, and response headers.



    init?(URL url: NSURL, statusCode statusCode: Int, HTTPVersion HTTPVersion: String?, headerFields headerFields: [String : String]?)


    - (instancetype _Nullable)initWithURL:(NSURL * _Nonnull)url statusCode:(NSInteger)statusCode HTTPVersion:(NSString * _Nullable)HTTPVersion headerFields:(NSDictionary<NSString *,NSString *> * _Nullable)headerFields



    The URL from which the response was generated.


    The HTTP status code to return (404, for example). See RFC 2616 for details.


    The version of the HTTP response as returned by the server. This is typically represented as "HTTP/1.1".


    A dictionary representing the keys and values from the server’s response header.

    Return Value

    An initialized NSHTTPURLResponse object or nil if an error occurred during initialization.


    Available in iOS 5.0 and later.

  • All HTTP header fields of the receiver. (read-only)



    var allHeaderFields: [NSObject : AnyObject] { get }


    @property(readonly, copy, nonnull) NSDictionary *allHeaderFields


    A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.

    The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.

    HTTP headers are case insensitive. To simplify your code, certain header field names are canonicalized into their standard form. For example, if the server sends a content-length header, it is automatically adjusted to be Content-Length.

    The returned dictionary of headers is configured to be case-preserving during the set operation (unless the key already exists with a different case), and case-insensitive when looking up keys.

    For example, if you set the header X-foo, and then later set the header X-Foo, the dictionary’s key will be X-foo, but the value will taken from the X-Foo header.

    Special Considerations

    Prior to OS X v10.7 and iOS 5, canonicalization occurred for all header fields. The case-preserving dictionary was first introduced in OS X v10.7.2 and iOS 5.


    Available in iOS 2.0 and later.