解析RESTAPI在Objective-C中上载二进制图像数据
我在尝试将图像数据发送到REST API时返回500错误解析RESTAPI在Objective-C中上载二进制图像数据,objective-c,rest,parse-platform,Objective C,Rest,Parse Platform,我在尝试将图像数据发送到REST API时返回500错误 NSMutableURLRequest *parseRequest = [NSMutableURLRequest requestWithURL:url]; [parseRequest setHTTPMethod:@"POST"]; [parseRequest setValue:@“*****” forHTTPHeaderField:@"X-Parse-Application-Id:"]; [parseRequest se
NSMutableURLRequest *parseRequest = [NSMutableURLRequest requestWithURL:url];
[parseRequest setHTTPMethod:@"POST"];
[parseRequest setValue:@“*****” forHTTPHeaderField:@"X-Parse-Application-Id:"];
[parseRequest setValue:@“*****” forHTTPHeaderField:@"X-Parse-REST-API-Key:"];
[parseRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type:"];
NSLog(@"All Headers: %@",[parseRequest allHTTPHeaderFields]);
NSData *imageData = [[NSData alloc]initWithData:[NSData dataFromBase64String:data]];
[parseRequest setHTTPBody:imageData];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSError *theJSONError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:parseRequest returningResponse:&theResponse error:&theError];
NSString *responseString = [[NSString alloc]initWithData:theResponseData encoding:NSUTF8StringEncoding];
NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:NSJSONReadingAllowFragments error:&theJSONError];
NSLog(@“Url to send request: %@“,url);
NSLog(@“Response String: %@",responseString);
NSLog(@"Response Error: %@",theError);
返回:
所有标题:
{
"Content-Type:" = "image/png";
"X-Parse-Application-Id:" = *****;
"X-Parse-REST-API-Key:" = *****;
}
Url to send request: https://api.parse.com/1/files/pic2.png
Response String: *HTML page with 500 error*
Response Error: (null)
我假设它不喜欢将二进制数据作为HTTPBody传递,但是由于我没有收到任何错误消息,所以很难判断发生了什么。用CURL发出同样的请求很好。像这样试试
NSMutableURLRequest *parseRequest = [NSMutableURLRequest requestWithURL:url];
[parseRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[parseRequest setHTTPShouldHandleCookies:NO];
[parseRequest setTimeoutInterval:30];
[parseRequest setHTTPMethod:@"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// 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=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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
[parseRequest setHTTPBody:body];
// set URL
[parseRequest setURL:requestURL];
由于没有要添加的参数,尝试了此文件的精简版本,但运气不佳
[body appendData:[[NSString stringWithFormat:@“内容处置:表单数据;文件名=\”image.jpg\“\r\n”]dataUsingEncoding:NSUTF8StringEncoding];[body appendData:[@“内容类型:图像/jpeg\r\n\r\n”数据使用编码:NSUTF8StringEncoding];[正文数据:图像数据];[body appendData:[[NSString stringWithFormat:@“\r\n”]dataUsingEncoding:NSUTF8StringEncoding]];}[body appendData:[[NSString stringWithFormat:@”--%@--\r\n,边界]数据使用编码:NSUTF8StringEncoding]]代码>