Search code examples
objective-cnsurlrequest

NSURLRequest setHTTPBody readonly


NSURLRequest *r = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]];
[r setHTTPBody:imageData];
r.HTTPBody = imageData;

Xcode gives an error on the second line: "No visible interface for 'NSURLRequest declares the selector 'setHTTPBody'"

and the third line "Assignment to readonly property"

but the class definition shows

/*! 
    @method HTTPBody:
    @abstract Sets the request body data of the receiver.
    @discussion This data is sent as the message body of the request, as
    in done in an HTTP POST request.
    @param data the new request body data for the receiver.
*/
@property (copy) NSData *HTTPBody;

Solution

  • A regular NSURLRequest is immutable. Use NSMutableURLRequest.