how to post parameter like {“register_id”:“3”} in AFNetworking

北城以北 提交于 2019-12-11 10:34:36

问题


i tried it but didn't work in AFNetworking only showing parameters error but i used postman to check and when i send data via key and value it showing error but from raw data i send {"register_id":"3"} then it will show me data so how to post parameter like this in AFNetworking.

using This Link

http://www.icubemedia.net/visitorbook/display_all.php

is any one can help me for that how to post that data

log error is:

2015-06-19 14:05:08.078 DemoAFNetworking[72771:1160924] {"msg":"parameter missing!"}


回答1:


Check this example on how to do a GET with simple parameter with AFNetworking 2.0:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"foo": @"bar"};

[manager GET:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

EDIT 1: added JSON serializer ;)




回答2:


Indeed there are no parameters missing, the fact that the request worked in Postman was the key. On the one hand, you should be trying to POST to that URL, not GET. On the other hand, since you are sending a JSON, you need the appropriate serializer.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//JSON Serializer
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"register_id": @"3"};

[manager POST:@"http://www.icubemedia.net/visitorbook/display_all.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Error: %@", error);
}];


来源:https://stackoverflow.com/questions/30931861/how-to-post-parameter-like-register-id3-in-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!