Alamofire发送json数据

Posted by Roy on 2017-04-08
185字预计1分

项目里用到人脸识别功能,就找了腾讯云的人脸识别,在调用接口时文档明确要求要json数据形式提交数据,我们在和第三方接口打交道经常会遇到这样的标准要求。
项目swift写的,请求框架用的是Alamofire在使用时没注意,遇到了点问题,注意下面的这段话

The JSONEncoding type creates a JSON representation of the parameters object, which is set as the HTTP body of the request. The Content-Type HTTP header field of an encoded request is set to application/json.

1
Alamofire.request("https://httpbin.org/get", parameters: parameters, encoding: URLEncoding.default)

在定义了Content-Type:”application/json”的httpheader头信息后,需要显式声明 encoding: URLEncoding.default 才会正确的采用json格式发送。


Kommentare: