***我对HTTP网络编程非常陌生。
基本上,这个命令可以工作: curl -k -L -v -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header 'Authorization: OAuth someAccessToken' --header "Force-Instance-Url: websiteurl" --header "Force-User-Id: someUserId" --data "{}" "websiteurl"
然而,在Postman(chrome的HTTP测试插件)或我在iOS中制作的程序中,我似乎无法成功获得响应。结果总是HTTP状态500-Java.io.EOFException:由于输入结束而没有内容映射到对象。
我一直非常小心地确保URL和标头完全相同,但没有任何运气。 在我的iOS程序中,我有一个HTTP请求,成功检索访问令牌,实例URL和force_user_id。我使用这些字段进行上述请求,但是响应是错误500。
既然在Postman中也无法正常工作,因此我不确定发布我的代码是否有帮助,但我仍然会发布:
NSURL *loginURL = [NSURL URLWithString:@"websiteurl"];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:loginURL];
[request setHTTPMethod:@"POST"];
NSString *instance_url = [self.loginDictionary objectForKey:@"instance_url"];
NSString *authorization = [NSString stringWithFormat:@"%@%@", @"OAuth ",[self.loginDictionary objectForKey:@"access_token"]];
NSString *instance_id = [self.loginDictionary objectForKey:@"id"];
NSRange range = [instance_id rangeOfString:@"/" options:NSBackwardsSearch];
if (range.location != NSNotFound)
{
instance_id = [instance_id substringFromIndex:range.location + 1];
}
[request setValue:@"application/json" forHTTPHeaderField: @"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField: @"Accept"];
[request setValue:authorization forHTTPHeaderField: @"Authorization"];
[request setValue:instance_url forHTTPHeaderField: @"Force-Instance-Url"];
[request setValue:instance_id forHTTPHeaderField: @"Force-User-Id"];
NSLog(@"\n\n%@", [request allHTTPHeaderFields]);
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
self.loginDictionary = (NSDictionary *) JSON;
NSLog(@"*************************************************************************");
NSLog(@"*************************************************************************");
NSLog(@"FIREWORKS");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"Request Failure Because %@",[error userInfo]);
}];
[operation start];
这是一个请求头的日志打印输出。
{
Accept = "application/json";
Authorization = "OAuth accessTokenValue";
"Content-Type" = "application/json";
"Force-Instance-Url" = "websiteurl";
"Force-User-Id" = someUserId;
}
最后,唯一让我感到有些线索的是curl命令中的auth header在单引号内,而其余部分则不是……我不确定这是否重要。
谢谢您的阅读和帮助。
由于输入结束,无法将内容映射到对象。
- jdbNSString *empty = @"{}"; NSData *requestData = [NSData dataWithBytes:[empty UTF8String] length:[empty length]]; [request setHTTPBody: requestData];
谢谢@jdb! - Sethypie