objective-cjsoncocoa-touchasiformdatarequest

ASIFormDataRequest with setPostValue and Method DELETE


I am trying to use ASIFormDataRequest with setPostValues and Method DELETE, Following are the codes lines

NSString *urlstring = [NSString stringWithFormat:@"%@%@",notificationsURL, typeId];
NSLog(@"Notification URL == %@",urlstring);
NSURL *urlR = [NSURL URLWithString:urlstring];

notificationAPNRequest = [ASIFormDataRequest requestWithURL:urlR];
notificationAPNRequest.timeOutSeconds = 30;
notificationAPNRequest.useSessionPersistence = NO;

[notificationAPNRequest setPostValue:deviceId forKey:@"deviceId"];
[notificationAPNRequest setPostValue:apnToken forKey:@"apnToken"];

if ([methodStr isEqualToString:@"OFF"]) {
    [notificationAPNRequest setRequestMethod:@"DELETE"];
}
else if ([methodStr isEqualToString:@"ON"]) {
    [notificationAPNRequest setRequestMethod:@"PUT"];
}

[notificationAPNRequest setDelegate:self];
[notificationAPNRequest setDidFinishSelector:@selector(notificationAPNSuccess:)];
[notificationAPNRequest setDidFailSelector:@selector(notificationAPNFailure:)];

[notificationAPNRequest startAsynchronous];

In Success method i get the Code 405 NSLog(@"%d",[request responseStatusCode]);

While when i hit from postman client then its works. Please let me know where i did mistake.


Solution

  • When you are using DELETE method with some post parameters you need to call "buildPostBody" function before setting Http DELETE method like below

    [notificationAPNRequest setPostValue:deviceId forKey:@"deviceId"]; [notificationAPNRequest setPostValue:apnToken forKey:@"apnToken"];

    [notificationAPNRequest buildPostBody]; /// Call this before setting request method

    [notificationAPNRequest setRequestMethod:@"DELETE"];

    Hope it works now