AFNetworking 2.0 Send Post Request with URL Parameters

后端 未结 6 2169
忘掉有多难
忘掉有多难 2020-12-19 05:16

How do I send a POST request with AFNetworking 2.0 with all the parameters in the URL like such:

http://www.myserver.com?api_key=something&

6条回答
  •  被撕碎了的回忆
    2020-12-19 05:45

    I've had the same problem with you.

    Other people's answers seem to be accurate.

    The cause of the error is:

    when you init NSUrl with parameters such as http:www.baidu.com/api?id=1&name=我,it needs you to encode your urlString with utf-8

    such as :

    //解决奇葩需求:请求方式为post时,url带?id=1之类。主要原因是url带中文的时候url初始化失败 URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
    

    Hope to help you!

提交回复
热议问题