Search code examples
iosasihttprequestnsurlrequest

NSURLRequest HTTPBody vs. ASIHTTPRequest postBody


I'm trying to convert most of a project's NSURLRequests that use NSURLConnection to ASIHTTPRequest calls. I came across an issue about setting the HTTPBody in an ASIHTTPRequest. Here's what I had for the NSURLRequest call:

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[self.paramString dataUsingEncoding:NSUTF8StringEncoding]];
[self _sendRequest:req];

And to convert to ASI, this is what I have so far:

__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:self.urlString]];
[request setRequestMethod:@"POST"];
[request appendPostData:[self.paramString dataUsingEncoding:NSUTF8StringEncoding]];
[request setCompletionBlock:^{
    NSLog(@"ASIHTTP request finished: %@", [request responseString]);
    // Do stuff here
}];
[request setFailedBlock:^{
    NSError *error = [request error];
    NSLog(@"ASIHTTP error: %@", [error description]);
}];
[request startAsynchronous];

Although when I run this with ASI, I get this error:

ASIHTTP error: Error Domain=ASIHTTPRequestErrorDomain Code=6 "Unable to start HTTP connection" UserInfo=0x10ddf6b0 {NSLocalizedDescription=Unable to start HTTP connection}

EDIT This error was fixed, but data is still not being transmitted correctly due to the body not being set.

I'm thinking this has to do with me not setting the body correctly. I tried using ASI's setPostBody, but that only produced the same result. This works fine with NSURLRequest, but not ASI. I'm pretty sure it's really simple and I just haven't explored ASI's full library quite yet, but I was just wondering if anyone had any suggestions. I have read the documentation, but I couldn't find what I was looking for.

Thanks!


Solution

  • I ended up using ASIFormDataRequest and setting values and keys. That seemed to work!