1

I am trying to upload photo with HTTP POST request.

This my code:

// Value for arguments
NSDictionary * params = @{@"caption":@"%23me%20hello%20world"};
NSURL * url = [NSURL URLWithString:@"http://api.airshipdock.com/uploadPhoto?access_token=%@",access_token,nil];

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",BoundaryConstant,nil] forHTTPHeaderField: @"Content-Type"];

// Create body
NSMutableData * body = [NSMutableData data];
for (NSString *param in params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if (sendData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"photo.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Transfer-Encoding: binary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

[request setURL:url];
[request setHTTPBody:body];
[request setValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField:@"Content-Length"];

NSURLResponse * response;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];

Here is generated request:

POST /uploadPhoto?access_token=dd883d348d1a389dddaa91f8e1dd8f5f43dd8f4dd8f5d686e34c8a373f328c3 HTTP/1.1
Host: api.airshipdock.com
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=V2ymHFg03ehbqgZCaKO6jy
Content-Length: 135597
Accept-Language: en-us
Accept: */*
Connection: keep-alive
User-Agent: Pictography/1.0 CFNetwork/609 Darwin/12.2.0

--V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="caption"

%23me%20hello%20world
--V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="file"; filename="photo.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

< here are image bytes >
--V2ymHFg03ehbqgZCaKO6jy--

Server answers:

{
    "error": {
        "code":400,
        "type":"PHOTO_SAVE_FILE_INVALID",
        "description":"",
        "request_params": [
            {"key":"method","value":"uploadPhoto"}
            {"key":"access_token","value":"dd883d348d1a389dddaa91f8e1dd8f5f43dd8f4dd8f5d686e34c8a373f328c3"}
        ]
    }
}

Looking at request_params I see server can't see POST params caption and file. Whats the problem with my request?

Here is server API documentation: LINK

k06a
  • 17,755
  • 10
  • 70
  • 110
  • The request is POST and then you append params as like for a GET? – ott-- Nov 03 '12 at 19:37
  • Sending all params via POST gives me error "ACCESS_TOKEN_EMPTY". I think server don't see POST arguments at all. – k06a Nov 03 '12 at 19:46
  • I've done it like this: `NSString *requestBody = [NSString stringWithFormat:@"key1=%@&key2=%@&key3=%@", ...` and then `[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];`. – ott-- Nov 03 '12 at 20:46
  • @ott, Is it possible to add file this way? – k06a Nov 03 '12 at 20:47
  • Look at this with 2 examples: http://stackoverflow.com/questions/125306/how-can-i-upload-a-photo-to-a-server-with-the-iphone – ott-- Nov 03 '12 at 21:43

0 Answers0