使用 AFNetworking 的 POST 请求出现错误 404
Posted
技术标签:
【中文标题】使用 AFNetworking 的 POST 请求出现错误 404【英文标题】:Error 404 for POST Request using AFNetworking 【发布时间】:2015-02-19 16:01:45 【问题描述】:我收到此错误,但不知道为什么:/
2015-02-19 10:53:45.005 Agendize[4233:60b] Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0x1782e4980 com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x17022e3c0> URL: https://mylink status code: 404, headers
Connection = "keep-alive";
"Content-Length" = 104;
Date = "Thu, 19 Feb 2015 16:12:32 GMT";
Server = "nginx/1.1.19";
这是我的 xcode 请求:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parametersDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[request setHTTPMethod:@"POST"];
[request setValue:@"Basic: someValue" forHTTPHeaderField:@"Authorization"];
[request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
请帮忙!!!
【问题讨论】:
所以...你的错误是告诉你你的请求没有找到你正在寻找的任何东西。使用您提供的占位符数据,无法确定问题所在。使用 REST 客户端来测试您的端点和请求。查看Postman 【参考方案1】:NSError *error=nil;
NSString *url = @"Your URL to post data";
NSData *jsonRequestDict= [NSJSONSerialization dataWithJSONObject:userDict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonCommand=[[NSString alloc] initWithData:jsonRequestDict encoding:NSUTF8StringEncoding];
NSLog(@"***jsonCommand***%@",jsonCommand);
NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys:jsonCommand,@"requestParam", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //AFHTTPResponseSerializer serializer
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSError *error=nil;
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful, response '%@'", responseStr);
NSMutableDictionary *jsonResponseDict= [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
NSLog(@"Response Dictionary:: %@",jsonResponseDict);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
【讨论】:
能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?以上是关于使用 AFNetworking 的 POST 请求出现错误 404的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking - 如何通过 ID 发出 POST 请求
使用 NSURLSession 复制 AFNetworking POST 请求
使用 AFNetworking 的 POST 请求出现错误 404