2015-10-14 157 views
0

這裏發送JSON數據的代碼是我嘗試做的,它返回未經授權IOS如何使用HTTP POST請求

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]]; 
[manager.requestSerializer setValue:@"application/json"forHTTPHeaderField:@"Content-Type"]; 

NSDictionary *parameters = @{@"user": @"name",@"email": @"[email protected]"}; 

[manager POST:@"http://testgcride.com:8081/v1/users" parameters:parameters 

success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     NSLog(@"JSON: %@", responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 
+0

我猜你調用的API testgcride需要某種授權頭的要與請求一起發送? –

回答

0

The URL you're using需要用戶名和密碼才能訪問。

它看起來就像是一個HTTP Basic認證,所以你應該能夠獲得證書,並將它們設置:

[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"username" password:@"password"]; 
+0

仍然顯示我錯誤:( – user2533699