在AFNetworking 2.x中设置自定义标头后,RESTful API不会接收参数 [英] After setting custom headers in AFNetworking 2.x, the parameters are not being received by the RESTful API

查看:247
本文介绍了在AFNetworking 2.x中设置自定义标头后,RESTful API不会接收参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 AFNetworking 2.0将标头添加到GET请求中遵循建议的解决方案使用以下代码段为请求指定自定义标头:

I followed the suggested solution at AFNetworking 2.0 add headers to GET request to specify custom headers for the request with the following code snippet:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:someID forHTTPHeaderField:@"some_id"];
NSDictionary *parameters = @{@"id": user.id, @"birthday": user.birthday};
[manager POST:[NSString stringWithFormat:@"%@/user_create",BaseURLString] parameters:parameters
 success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     if (responseObject[@"error"])
     {
         NSLog(@"REST User Create Response Error: %@", responseObject[@"error"]);
     }
     else
     {
         [self saveUserDetails:responseObject];
     }
 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSLog(@"REST User Create Error: %@", error);

 }];

但是执行此操作时会发生什么情况,就是我在API响应中收到一条错误消息,指出所有参数丢失。以前曾经使用过相同的代码块(无需设置自定义标头以及当API最初不需要它们时)。

But what happens when this gets executed is I get an error in the response from the API stating all my parameters are missing. This same block of code used to work before (without setting the custom header and when the API didn't require them originally).

有人知道如何正确地设置两个自定义标头和POST参数?

Does anybody know how to properly set both custom headers and POST parameters?

谢谢,
Nino

Thanks, Nino

推荐答案

我发现我应该将请求序列化程序设置为以下内容,因为我的API不需要JSON格式的参数。

I discovered that I should have set my request serializer to the following instead since my API doesn't require JSON-formatted parameters.

manager.requestSerializer = [AFHTTPRequestSerializer serializer];

希望这有助于其他人处理与我相同的情况。

Hope this helps others dealing with the same situation as mine.

这篇关于在AFNetworking 2.x中设置自定义标头后,RESTful API不会接收参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆