NSURLRequest导致500错误

时间:2014-04-01 17:32:37

标签: ios xcode cocoa-touch nsurlrequest

我尝试使用NSURL请求进行api调用上传图像,但我仍然遇到500错误。为了向您展示我在这里尝试完成的是curl命令的样子。

$ curl -i -F api_password=<YOUR_API_PASSWORD> -F file=@<LOCAL_FILE_PATH> https://upload.api.com/

这是我到目前为止所得到的,我继续得到500个错误。你能不能指点我做错的事(我没有NSURLRequest主人)。

NSString *apiPass = @"apiKey";
NSString *filePath = imagePath;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://upload.wistia.com"]];
[request setHTTPMethod:@"POST"];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

// Create data string
NSString *dataString = [NSString stringWithFormat:@"api_password=%@&file=@%@", apiPass, filePath];

NSData *postData = [dataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

[request setHTTPBody:[dataString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse * response = nil;
NSError * error = nil;

[NSURLConnection sendSynchronousRequest:request
                      returningResponse:&response
                                  error:&error];

3 个答案:

答案 0 :(得分:2)

尝试撰写类似于此的POST请求:

NSString *boundary = @"---###-----##----##--#----###---BOUNDARY---###";
NSMutableData *postBody = [NSMutableData data];
NSString *postString = nil;

postString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
postString = [postString stringByAppendingString:@"Content-Disposition: form-data; name=\"api_password\"\r\n\r\n"];
postString = [postString stringByAppendingString:apiPass];

postString = [postString stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n", boundary]];
postString = [postString stringByAppendingString:@"Content-Disposition: form-data; name=\"file\"\r\n\r\n"];
postString = [postString stringByAppendingString:filePath];

postString = [postString stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary]];

[postBody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://upload.wistia.com"] 
                                                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                   timeoutInterval:60.0f];
[request setHTTPMethod:@"POST"];    

[request setValue:@"*/*" forHTTPHeaderField:@"Accept"];   
[request setValue:[@"multipart/form-data; boundary=" stringByAppendingString:boundary] forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", postBody.length] forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:postBody];

答案 1 :(得分:0)

Google for&#34;错误500&#34;。它的内部服务器错误&#34;。这是服务器上的错误。除了向服务器人员发送电子邮件之外,您无能为力。

答案 2 :(得分:0)

当我想将“photo.jpg”上传到某个服务器时,我使用这样的代码:

UIImage *image = [UIImage imageNamed:@"photo.jpg"];

NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"%@", endItemBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

在这种情况下,图像的数据表示将被加载而不是链接到图像。 你可以试试这样的事情。