curl可以正常工作,但在iOS或Postman中无法使用HTTP请求

4

***我对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在单引号内,而其余部分则不是……我不确定这是否重要。

谢谢您的阅读和帮助。

1个回答

3

curl会自动添加一些头部信息。在您的示例中,它将发送 Host: websiteurlContent-Length: 2


嗯,我尝试添加了这两个头文件,但是我仍然收到相同的错误。还是谢谢你。 - Sethypie
你的代码中应该在哪里发送空对象({})?错误提示为:由于输入结束,无法将内容映射到对象。 - jdb
抱歉,我不理解这个问题。什么空对象? - Sethypie
有人帮我通过在表单数据字段中输入 {} 来使 Postman 工作!这似乎与您所说的类似。您有任何关于如何在 iOS 中实现此操作的想法吗? - Sethypie
1
问题通过添加以下内容解决:NSString *empty = @"{}"; NSData *requestData = [NSData dataWithBytes:[empty UTF8String] length:[empty length]]; [request setHTTPBody: requestData];谢谢@jdb! - Sethypie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接