我想使用AFNetworking对sinatra项目进行POST以获取json数据。 AFNetwroking总是返回403状态代码。但卷曲的效果与预期的一样。为什么呢?
以下是Xcode上的错误:
<AFHTTPRequestOperation: 0x7ffd5da42780, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0x7ffd5d9591b0> { URL: http://127.0.0.1:9292/api/v1/events/new }, response: <NSHTTPURLResponse: 0x7ffd5b7b7030> { URL: http://127.0.0.1:9292/api/v1/events/new } { status code: 403, headers {
Connection = "Keep-Alive";
"Content-Length" = 69;
"Content-Type" = "application/json";
Date = "Fri, 23 Jan 2015 08:42:32 GMT";
Server = "WEBrick/1.3.1 (Ruby/2.1.5/2014-11-13)";
"X-Content-Type-Options" = nosniff;
} }>
终端卷曲:
curl http://127.0.0.1:9292/api/v1/events/new
{"error_code":10001,"error_message":"need token or token is illegal"}%
IOS代码不起作用:
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:
[NSURL URLWithString:@"http://127.0.0.1:9292/"]];
[manager POST:@"api/v1/events/new"
parameters:nil
success:nil
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{DLog(@"%@",operation);}];
rackup log:
[2015-01-23 16:58:20] INFO WEBrick 1.3.1
[2015-01-23 16:58:20] INFO ruby 2.1.5 (2014-11-13) [x86_64-darwin14.0]
[2015-01-23 16:58:20] INFO WEBrick::HTTPServer#start: pid=14445 port=9292
127.0.0.1 - - [23/Jan/2015:16:58:36 +0800] "POST /api/v1/events/new HTTP/1.1" 403 69 0.0179
答案 0 :(得分:0)
实际上它工作正常,我的服务器端做了一个路径过滤器,但这里提供了一种方法,有时不仅打印operation
而且operation.responseObject
我得到了我想要的东西。
{
"error_code" = 10001;
"error_message" = "need token or token is illegal";
}