JSON正文AFNetworking 2.0的POST请求 [英] POST request with JSON body AFNetworking 2.0
本文介绍了JSON正文AFNetworking 2.0的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
反正有使用AFNetworking〜> 2.0发送带有JSON正文的POST请求吗?
Is there anyway to send a POST request with a JSON body using AFNetworking ~> 2.0?
我尝试使用:
manager.requestSerializer = [AFJSONRequestSerializer序列化器];
经理POST:< url>参数:@ {@数据:@值}成功:< block>失败:< block>’
,但是它不起作用。任何帮助是极大的赞赏。
谢谢
but it doesn't work. Any help is greatly appreciated. Thanks
推荐答案
您可以在 NSMutableURLRequest
在参数中不直接输入:
。参见我的示例代码:
You can add your JSON body in NSMutableURLRequest
not direct in parameters:
. See my sample code :
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// Set post method
[request setHTTPMethod:@"POST"];
// Set header to accept JSON request
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// Your params
NSDictionary *params = @{@"data":@"value"};
// Change your 'params' dictionary to JSON string to set it into HTTP
// body. Dictionary type will be not understanding by request.
NSString *jsonString = [self getJSONStringWithDictionary:params];
// And finally, add it to HTTP body and job done.
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:<block> failure:<block>];
希望这对您有所帮助。编码愉快! :)
Hope this will help you. Happy coding! :)
这篇关于JSON正文AFNetworking 2.0的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文