• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 752
  • Last Modified:

NSMutableURLRequest adding extra information to the body, making Content-Length incorrect

I'm using an NSMutableURLRequest to perform a simple file upload from iOS5 to a custom server. When I send the request, I have it print out the length of bodyContents, which contains the body of the request. For the particular request I am working on, it will say that the length is 46784. I don't set the content-length, since I found out it was doing that automatically, but I pull the info anyway in case I need it later. The headers being received by the server say that the content-length is 46784. When I do a character count on the body at the server end, it tells me that the length of the body is 46788, 4 too many. I ran another request, again, the request had 4 more characters than what I sent.

At first, I thought it had to be on the server side, just because it didn't make any sense. So I sent a request from a test program that I know works to the server, and checked the length of the body it sent against the content-length header and they were identical.

What is the best way to deal with this? Should I manually set the content-length to 4+[bodyContents length]? I don't think that would be what I would want to do, just because I shouldn't have to add extra space for data I don't know that I want.

Here is the code that sends the request

-(void)send:(id)delegate
{
    bodyContents = [bodyContents stringByAppendingFormat:@"--%@--",boundry];
    NSInteger length = [bodyContents length];
    NSLog(@"Length is %i",length);
    [request setHTTPBody:[bodyContents dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:delegate];
    if(connection)
    {
        NSLog(@"Connection good");
    }
    else
    {
        NSLog(@"Connection bad");
    }
}

Open in new window


The content-type is set earlier in the class, but the content-length header is never touched.
0
Curtis Long
Asked:
Curtis Long
1 Solution
 
Curtis LongAuthor Commented:
I ran a diff on the working request from the test program and the request from iOS and found a couple changes. I went with ignoring the 4 extra characters and just fixed a couple really simple things (adding another - in some places, correcting spelling mistakes, the ones I really hate) and now everything works great
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now