我创建了一个NSURLRequest,可以解雇请求。我已将它连接到requestbin,但不是requestbin解析相关的表单数据,而是将其视为原始主体。
我有一个正常工作的CURL命令,并且我可以告诉两个请求原始主体看起来一样 - 我无法弄清楚在Obj-c中我需要改变什么来获取那些形式参数才能正常工作。
这是requestbin的inspect链接:http://requestb.in/1iszums1?inspect
工作CURL:
curl -X POST -H "Authorization: Bearer token" -H "X-ADN-Pretty-JSON: 1" -F "type=com.example.test" -F "content=@/Users/me/Desktop/test.jpg" "http://re
questb.in/1iszums1"
的OBJ-C
NSString* url = @"http://requestb.in/1iszums1";
UIImage* img = [UIImage imageNamed:@"test.jpg"];
NSData *imageData = UIImageJPEGRepresentation(img, 1.0f);
NSString* token = @"token";
NSString* name = @"test.jpg";
NSString* type = @"com.example.test";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"------------------------------6c491b1c62c8";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
[request setValue:@"1" forHTTPHeaderField: @"X-ADN-Pretty-JSON"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:@"%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"type\"\r\n\r\n%@\r\n", type] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"content\"; filename=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSString *responseBody = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@ %@", response, responseBody);
}
}];
答案 0 :(得分:0)
没有尝试过,我的猜测是你在边界上缺少几个破折号: 变化
[body appendData:[[NSString stringWithFormat:@"%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
到
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
很难保持所有的破折号。如果在内容类型中声明的边界字符串是" foo"那么你需要将每个部分与" - foo"并使用" - foo - "
结束整个表单数据