YTKNetwork icon indicating copy to clipboard operation
YTKNetwork copied to clipboard

ytknetwork怎么处理post中x-www-form-urlencoded这种类型请求数据

Open fandeng opened this issue 5 years ago • 2 comments

提出问题前请先确认完成了下列几项步骤 New Issue Checklist

  • [ ] 我已经阅读过 贡献指南 I have read and understood the CONTRIBUTING guide
  • [ ] 我已经阅读过 程序文档 I have read the Documentation
  • [ ] 我已经证实这个问题来源于 YTKNetwork 本身,而不是其所依赖的 AFNetworking I have confirmed that this issue is caused by YTKNetwork, not AFNetworking
  • [ ] 我已经在项目的 问题列表 中搜索过并且没有找到类似问题 I have searched for a similar issue in the project and found none

问题描述 Issue Description

请给出所提出问题的详尽描述,包括具体的错误信息,打印的堆栈等等,以及重现此问题的具体步骤。

Please fill in the detailed description of the issue (full output of any stack trace, compiler error, ...) and the steps to reproduce the issue.

fandeng avatar Oct 25 '18 01:10 fandeng

这个类型有点坑,我自己试出来的。。

需要把参数拼成"="形式,然后放进数组里才行

  • (id)requestArgument { return @[[NSString stringWithFormat:@"phone=%@",phone], [NSString stringWithFormat:@"code=%@",code]]; }

然后在- (NSURLRequest *)buildCustomUrlRequest { . . ...... NSString *jsonBodyStr = [((NSArray *)self.requestArgument) componentsJoinedByString:@"&"]; request.HTTPBody = [jsonBodyStr dataUsingEncoding:NSUTF8StringEncoding]; return request; }

这样就可以了

Free-am avatar Dec 07 '20 03:12 Free-am

补充一个swift版本的:

override func buildCustomUrlRequest() -> URLRequest? {
        let url = URL.init(string: requestUrl())!
        var req = URLRequest.init(url: url)
        req.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        req.httpMethod = "POST"
        let args = requestArgument()
        var params = [String]()
        for key in args.keys {
            let value = "\(args[key]!)".addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
            params.append("\(key)=\(value)")
        }
        let bodyStr = params.joined(separator: "&")
        req.httpBody = bodyStr.data(using: .utf8)
        return req
}

xx-li avatar Aug 10 '21 08:08 xx-li