I have a setup where I'm using ASIHttpRequest to make service calls in a background NSOperation subclass. I call a method from my main() in the NSOperation and have that method go through and build out my request with appropriate URL, headers, body, etc. The problem is that I'm occasionally seeing the request body set to null instead of the expected value. I开发者_运维问答 print out what the expected body should be right before setting it to the request and that representation is NOT null, so I don't know where/how the request body for the ASIHttpRequest gets released.
Here's some sample code of how I'm setting the request body ... this method gets called by another driver method that controls the whole networking workflow. That method, in turn, is called by the main() method of my NSOperation.
+ (ASIHTTPRequest*) buildRequestForProjectModify: (ANVideoProject*) theProject {
if (theProject.selfUrl == nil) return nil; //Can't do anything if we don't have a project url.
NSString* projectPutUrl = theProject.selfUrl;
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:projectPutUrl]];
[ANAppsServiceHelper addStandardHeadersToRequest:request];
NSDictionary* projectDict = [theProject jsonFriendlyForSaveAndPreview];
ANLogInfo(@"\n\nProject Post body: \n%@\n\n", projectDict); //This will print out ok
ANLogInfo(@"\n\nProject Post Body as JSON\n%@\n\n", [projectDict JSONRepresentation] ); //this too prints out ok.
NSString* jsonRep = [projectDict JSONRepresentation];
NSData* pd = [jsonRep dataUsingEncoding:NSUTF8StringEncoding];
[request appendPostData:pd];
[request setRequestMethod:@"PUT"];
return request; //I have a problem in the calling method from here where the request body is now null.
}
I read around about setting up an autorelease pool in the main method of my NSOperation subclass, and so I am doing that at the top of the main method. When I did that, the occurrence of (null) for the request body was mitigated, but still happened roughly 1/4 of the time I invoked this operation. Strange thing is that no other part of the request ever seems to get set to null in such a random fashion (ie request method or headers). Here is log output from the driver method I mention above printing out the request that was returned:
Here is Modify the request: https://<service-url>/projects/p0FvVjc790MFWduhhqUStA
Here is the request headers: {
Accept = "application/json";
"Accept-Encoding" = gzip;
Authorization = "Bearer <key>";
"Content-Length" = 293;
"Content-Type" = "application/json";
"User-Agent" = "iPhone (OS 4.3.2)";
}
Here is the request method: PUT
Here is the request body: (null)
Update:
So one thing I'm noticing is that sometimes the request body is null and also sometimes there is some garbage data added on to the end of the request body (and sometimes the request body is totally fine). Even more strangely, the request actually always goes through successfully with the body that I intended to add (even if the printout looks bad). This suggests that things are ok in the ASIHttpRequest but perhaps there is something going on with how I am printing out the state of my request or some other issue between ASIHttpRequest and the SBJSon library I'm using that causes the request to look corrupted from time to time (even though it apparently is not).
Did you print out the content of pd (NSData *)? Is the size very big? Then you may need to stream post data from disk:
[request setRequestMethod:@"PUT"];
[request setPostBodyFilePath:@"/Users/ben/Desktop/another-big-one.txt"];
[request setShouldStreamPostDataFromDisk:YES];
精彩评论