[英]iOS Get JSON response from AFNetworking HTTP POST method with JSON parameters
I am getting a 404
error while posting with JSON parameters at the https://api.hackerearth.com/codemonk/v1/topicdetail/
. 在
https://api.hackerearth.com/codemonk/v1/topicdetail/
上使用JSON参数发布时出现404
错误。 The server uses POST HTTP method to get a topic
's details & JSON response is expected when successful. 服务器使用POST HTTP方法获取
topic
的详细信息,并且成功时需要JSON响应。 The POST parameter is id
of the topic
object. POST参数是
topic
对象的id
。 POST Parameters are expected to be in JSON. POST参数应为JSON。 I am using AFNetworking as follows -
我正在使用AFNetworking如下-
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *params = [NSDictionary dictionaryWithObject:@1 forKey:@"id"];
NSString *str = @"https://api.hackerearth.com/codemonk/v1/topic-detail/";
NSString *encodedStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[manager POST:encodedStr
parameters:params
success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@"responseObject : %@",responseObject);
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
NSLog(@"error : %@", error.localizedDescription);
}];
This is regular stuff but don't know why I can't seem to get it correct now. 这是常规内容,但是不知道为什么我现在似乎无法正确处理。 I am only getting a
404 Page Not Found
error. 我仅收到
404 Page Not Found
错误。 This is not a server side issue for sure. 当然,这不是服务器端的问题。 Any help guys ?
有帮助吗?
Maybe this will help. 也许这会有所帮助。
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
operationManagerInstance.requestSerializer = requestSerializer;
=============UPDATE =============更新
I have 404 when copying your URL. 复制您的网址时,我有404。 It's because hyphen symbol between topic-detail is not actually hyphen.
这是因为主题细节之间的连字符实际上不是连字符。 It's some special character that doesn't work.
这是一个特殊字符,不起作用。
https://api.hackerearth.com/codemonk/v1/topic -detail/ https://api.hackerearth.com/codemonk/v1/topic -detail /
Instead I deleted it and typed hyphen manually and it works fine. 相反,我删除了它并手动键入连字符,它可以正常工作。
Remove the following line 删除以下行
NSString *encodedStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Try the following: 请尝试以下操作:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *params = [NSDictionary dictionaryWithObject:@1 forKey:@"id"];
NSString *str = @"https://api.hackerearth.com/codemonk/v1/topic-detail/";
[manager POST:str
parameters:params
success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@"responseObject : %@",responseObject);
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
NSLog(@"error : %@", error.localizedDescription);
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.