ios如何使用http post请求发送json数据

时间:2015-10-14 20:54:22

标签: ios json http-post afnetworking

这是我尝试做的代码,它会返回未经授权的

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

NSDictionary *parameters = @{@"user": @"name",@"email": @"a@gmail.com"};

[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);
    }];

1 个答案:

答案 0 :(得分:0)

The URL you're using需要用户名和密码才能访问。

它看起来像是HTTP基本身份验证,因此您应该能够获取凭据并进行设置:

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