HTTP请求适用于curl,但不适用于iOS或Postman
问题描述:
***我对HTTP网络很陌生。HTTP请求适用于curl,但不适用于iOS或Postman
基本上这样工作: 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:由于输入结束而无内容映射到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命令的权威性标题是单引号,但其余的都没有......我不知道如果这很重要或不重要。 感谢您的阅读和帮助。
答
卷曲会自动添加一些标题。在你的例子中,它会发送Host: websiteurl
和Content-Length: 2
嗯我试着添加这两个头,但我仍然收到相同的错误。不过谢谢。 – Sethypie
你在哪里发送代码中的空对象({})?该错误说:'没有内容映射到对象由于输入结束。' – jdb
对不起,我不明白这个问题。什么是空对象? – Sethypie