HTTP请求适用于curl但不适用于iOS或Postman

时间:2013-06-28 00:38:59

标签: ios http curl

***我是HTTP网络的新手。

基本上,这有效: curl -k -L -v -X POST --header“Content-Type:application / json”--header“Accept:application / json”--header'授权:OAuth someAccessToken'--header“Force-Instance-Url :websiteurl“--header”Force-User-Id:someUserId“ - data”{}“”websiteurl“

然而,我似乎无法在Postman(Chrome的HTTP测试插件)或我在iOS中制作的程序中获得响应。结果始终是HTTP状态500 - java.io.EOFException:由于输入结束,没有要映射到Object的内容。

我一直非常小心地确保网址和标题完全相同但没有运气。 在我的iOS程序中,我有一个http请求,成功检索访问令牌,instance_url和force_user_id。我使用这些字段来发出上面发布的请求,但响应是错误500。

由于它在邮递员中不起作用,我不确定发布我的代码是否有帮助,但我会反正:

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头是单引号,但其余的不是......我不确定这是否重要或不。 感谢阅读和帮助。

1 个答案:

答案 0 :(得分:3)

curl会自动添加一些标题。在您的示例中,它会发送Host: websiteurlContent-Length: 2