使用iOS上的HTTP POST将照片上传到服务器

时间:2012-11-03 18:55:08

标签: ios http post

我正在尝试使用HTTP POST请求上传照片。

这是我的代码:

// 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];

这是生成的请求:

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--

服务器答案:

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

查看request_params我看到服务器无法看到POST参数captionfile。我的要求是什么问题?

以下是服务器API文档:LINK

0 个答案:

没有答案