iosobjective-cweb-servicesrestasihttprequest

PUT request using ASIHTTP?


I have a web-service which is using PUT method to request from the URL default/isUserInternational/id/1 and in the request body should append isInternational=1.

I'm using following code and still I could not make PUT request.

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"default/isUserInternational/id/%@", @"5"]];
self.asiFormDataRequest = [[ASIFormDataRequest alloc] initWithURL:url];
[self.asiFormDataRequest setDelegate:self];
[self.asiFormDataRequest setTimeOutSeconds:REQUEST_TIMEOUT];
[self.asiFormDataRequest setPostFormat:ASIMultipartFormDataPostFormat];
[self.asiFormDataRequest setRequestMethod:@"PUT"];
[self.asiFormDataRequest addRequestHeader:@"Accept" value:@"application/json"];
[self.asiFormDataRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];

//[self.asiFormDataRequest addRequestHeader:@"1" value:@"isInternational"];
//[self.asiFormDataRequest appendPostData:[@"1" dataUsingEncoding:NSUTF8StringEncoding]];

[self.asiFormDataRequest addPostValue:@"1" forKey:@"isInternational"];
[self.asiFormDataRequest startSynchronous];

Solution

  • Although If you used appendPostData: or appendPostDataFromFile: it wont work. Plus if you used them and it will considers as POST request as mentioned in the documentation.

    // Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:

    ( http://allseeing-i.com/ASIHTTPRequest/How-to-use#setting_request_headers).

    So Im posting correct code which has worked for me, hope it might helpful for others!

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"default/isUserInternational/id/%@", @"5"]];
    self.asiFormDataRequest = [[ASIFormDataRequest alloc] initWithURL:url];
    [self.asiFormDataRequest setDelegate:self];
    [self.asiFormDataRequest setTimeOutSeconds:REQUEST_TIMEOUT];
    //[self.asiFormDataRequest setPostFormat:ASIMultipartFormDataPostFormat];
    [self.asiFormDataRequest addRequestHeader:@"Accept" value:@"application/json"];
    [self.asiFormDataRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
    [self.asiFormDataRequest addRequestHeader:@"1" value:@"isInternational"];
    [self.asiFormDataRequest setRequestMethod:@"PUT"];
    
    [self.asiFormDataRequest addPostValue:@"1" forKey:@"isInternational"];
    [self.asiFormDataRequest startSynchronous];
    

    AND REMEMBER to remove the line [self.asiFormDataRequest setPostFormat:ASIMultipartFormDataPostFormat]; as it makes your request to be considered as a POST. That was silly mistake of mine!