2013-06-28 172 views
4

***我對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命令的權威性標題是單引號,但其餘的都沒有......我不知道如果這很重要或不重要。 感謝您的閱讀和幫助。

回答

3

捲曲會自動添加一些標題。在你的例子中,它會發送Host: websiteurlContent-Length: 2

+0

嗯我試着添加這兩個頭,但我仍然收到相同的錯誤。不過謝謝。 – Sethypie

+0

你在哪裏發送代碼中的空對象({})?該錯誤說:'沒有內容映射到對象由於輸入結束。' – jdb

+0

對不起,我不明白這個問題。什麼是空對象? – Sethypie

相關問題