af联网:如何在请求中设置内容类型的HTTP头?

时间:2022-08-22 19:39:16

The way to register my clients with my API server using command-line is

使用命令行来注册我的客户端。

curl -d'{"email": "e", "memberExternalId": "m"}' -H"Content-Type: application/json" https://api-myapp.com/register
{"memberId":"78f7f8a4-a8c9-454a-93a8-6633a1076781","clientId":"111322610106743984083443169763434312591","clientSecret":"255682200164339900511209955730378006963"}

I am trying to do similar thing with Objective-C using AFNetworking. My current code looks like

我正在尝试用AFNetworking来做与Objective-C类似的事情。我现在的代码是这样的。

- (void)connectWithServer {
    NSLog(@"connecting with server");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

    [manager POST:@"http://api-myapp.com/register"
       parameters:@{@"email": @"e", @"memberExternalId": @"m"}
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"JSON: %@", responseObject);
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error: %@", error);
            }];

}

What I see in my console is

我在控制台看到的是!

2014-11-15 15:13:29.719 myapp-ios[42377:70b] Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0xb61e130 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0xb3071b0> { URL: http://api-myapp.com/register } { status code: 400, headers {
    "Accept-Ranges" = none;
    Connection = close;
    "Content-Length" = 206;
    "Content-Type" = "text/html";
    Date = "Sat, 15 Nov 2014 23:13:29 GMT";
    Server = "WildFly/8";
    "X-Powered-By" = "Undertow/1";
} }, NSErrorFailingURLKey=http://api-myapp.com/register, NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<636f6d2e 66617374 6572786d 6c2e6a61 636b736f 6e2e636f 72652e4a 736f6e50 61727365 45786365 7074696f 6e3a2055 6e726563 6f676e69 7a656420 746f6b65 6e202765 6d61696c 273a2077 61732065 78706563 74696e67 20282774 72756527 2c202766 616c7365 27206f72 20276e75 6c6c2729 0a206174 205b536f 75726365 3a20696f 2e756e64 6572746f 772e7365 72766c65 742e7370 65632e53 6572766c 6574496e 70757453 74726561 6d496d70 6c403132 66336263 613b206c 696e653a 20312c20 636f6c75 6d6e3a20 375d>, NSUnderlyingError=0xb60f160 "Request failed: unacceptable content-type: text/html"}

What am I doing wrong? why the Content-Type is still text/html?

我做错了什么?为什么内容类型仍然是文本/html?

1 个解决方案

#1


4  

It's exactly what the error says: your server is sending back a webpage (HTML) for a 400 status code, when you were expecting JSON.

这正是错误的含义:当您期望JSON时,您的服务器正在为400个状态代码发送一个网页(HTML)。

A 400 status code is used a bad request, which is probably generated because you're sending URL-form-encoded text as application/json. What you really want is to use AFJSONRequestSerializer for your request serializer.

400个状态代码使用了一个糟糕的请求,这可能是由于您将url表单编码的文本作为应用程序/json发送而产生的。您真正需要的是为您的请求序列化器使用AFJSONRequestSerializer。

manager.requestSerializer = [AFJSONRequestSerializer serializer];

#1


4  

It's exactly what the error says: your server is sending back a webpage (HTML) for a 400 status code, when you were expecting JSON.

这正是错误的含义:当您期望JSON时,您的服务器正在为400个状态代码发送一个网页(HTML)。

A 400 status code is used a bad request, which is probably generated because you're sending URL-form-encoded text as application/json. What you really want is to use AFJSONRequestSerializer for your request serializer.

400个状态代码使用了一个糟糕的请求,这可能是由于您将url表单编码的文本作为应用程序/json发送而产生的。您真正需要的是为您的请求序列化器使用AFJSONRequestSerializer。

manager.requestSerializer = [AFJSONRequestSerializer serializer];