【问题标题】:HTTP request works in curl but not iOS or PostmanHTTP 请求适用于 curl 但不适用于 iOS 或 Postman
【发布时间】:2013-06-28 00:38:59
【问题描述】:

***我对 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: No content to map to Object due to end of input.

我一直非常小心地确保 url 和 headers 完全相同,但没有运气。 在我的 iOS 程序中,我有一个成功检索访问令牌、instance_url 和 force_user_id 的 http 请求。我使用这些字段来发出上面发布的请求,但响应是错误 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 标头是单引号,但其余的不是……我不确定这是否重要或不。 感谢您的阅读和帮助。

【问题讨论】:

    标签: ios http curl


    【解决方案1】:

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

    【讨论】:

    • 嗯,我尝试添加这两个标题,但仍然收到相同的错误。不过还是谢谢。
    • 您将代码中的空对象 ({}) 发送到哪里?错误说:No content to map to Object due to end of input.
    • 对不起,我不明白这个问题。什么空对象?
    • 其他人通过在表单数据字段中输入 {} 帮助我让邮递员工作!这似乎与您所说的相似。知道如何在 iOS 中执行此操作吗?
    • 问题通过添加解决:NSString *empty = @"{}"; NSData *requestData = [NSData dataWithBytes:[empty UTF8String] length:[empty length]]; [request setHTTPBody: requestData];Thanks@jdb!
    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 2015-09-19
    • 1970-01-01
    • 2022-01-21
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多