0

I have a web service. I use it to accept a base 64 string representation of a small (thumbnail size) image. This web service works awesome when using it with Fiddler and manually posting the request. When I run the same request with NSMutableURLRequest (or ASIHTTPRequest), it always returns a 413 status code (413 is Request Entity is Too Large).

Why would NSMutableURLRequest cause it to come up with a 413, whereas Fiddler returns 200 every time?

Here is my NSMutableURLRequest code. I could really use a push, if anybody has any ideas.

        //the image request
        NSMutableURLRequest *imageRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_IMAGE_API_URL] 
                                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                              timeoutInterval:240.0];

        //the post parameters
        [imageRequest setHTTPMethod:@"POST"];
        [imageRequest setHTTPBody:[imageMessage dataUsingEncoding:NSUTF8StringEncoding]];
        [imageRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];


        //a few other things
        NSURLResponse* imageresponse;
        NSError *imageerror;


        NSData* imageresult = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:&imageresponse error:&imageerror];
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)imageresponse;

        NSLog(@"imageresponse: %d", httpResponse.statusCode);
jdb1a1
  • 1,045
  • 1
  • 13
  • 32

2 Answers2

1

When I see this bit of your code:

//the image request
NSMutableURLRequest *imageRequest = 
    [NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_IMAGE_API_URL]  
                            cachePolicy:NSURLRequestUseProtocolCachePolicy   
                        timeoutInterval:240.0];

I'm guessing you have some whacky characters in your "POST_IMAGE_API_URL" #define, most likely in the parameters that you're passing along.

You need to URL encode the URL string you pass to your URL request.

Try doing:

// assuming POST_IMAGE_API_URL starts with a "@" character
NSString * yourURLAsString = [NSString stringWithString: POST_IMAGE_API_URL]; 
NSURL * yourEncodedURL = [yourURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

and pass "yourEncodedURL" in as a parameter to the URLRequest.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • thanks for the idea. There's nothing funny in my service URL string at all. I put together a simple, distilled service specifically for testing. I have the service's endpoint defined in a header file like this: #define POST_IMAGE_API_URL @"http://www.myserver.com/SRImageServ/SRImgService/" – jdb1a1 Jun 20 '12 at 16:37
  • well okay then! I see the trouble right off the bat. There's no scheme on that URL. Try "`http://myserver.com`..." – Michael Dautermann Jun 20 '12 at 16:39
  • Yeah, it's there. Sorry, the paste function got cut off. It really looks like this: #define POST_IMAGE_API_URL @"http://www.myserver.com/SRImageServ/SRImgService/" – jdb1a1 Jun 20 '12 at 16:41
  • Yep. I believe that your scheme is there now. Do you have control over the server side? Is it getting hit with the POST bytes at all? Is your server returning the 413 error code or is it coming from the iPhoneOS? – Michael Dautermann Jun 20 '12 at 16:43
  • No, the post data is never making it to the server. My trace object (this is a WCF service) tells me that I found the service, but that it never sends the data to the service. – jdb1a1 Jun 20 '12 at 16:54
  • Hmmmm... I'm about out of ideas. The only other thing that seems suspicious to me is that you're uploading image data but you gave it a MIME type of "`text/xml`", which I'm thinking might throw your server off in terms of what it's expecting to see (i.e. XML usually has nodes/fields/titles/etc., not just raw data). – Michael Dautermann Jun 20 '12 at 17:30
  • Thanks anyway, I appreciate the effort. I convert the image to a base64 string and then submit it via stream. The xml works perfectly when submitting from Fiddler (web debug tool). It just gives 413 when doing it from iPhone. The frustrating thing, is that I had this code working fine 2 months ago! – jdb1a1 Jun 20 '12 at 17:36
0

I found a solution for this. The issue was not on the Apple end, but on the IIS end. There is an additional parameter for IIS hosted applications (one of which being my WCF service) beyond what is specified in the WCF's web.config file that specifies the "uploadReadAheadSize" for each service. I increased this and the 413 went away. Interestingly enough, I didn't get this error when sending the HTTP request from Fiddler on a desktop client on the same network as the server where the service resides. Basically, I had the solution to this guy's problem but not his context. My solution was his context.

Community
  • 1
  • 1
jdb1a1
  • 1,045
  • 1
  • 13
  • 32