1

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

4

0 に答える 0