你做这个:
Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers).validate().responseJSON { ... }
事实上,它可以这样解构:
let request = Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers)
request.validate().responseJSON { ... }
request
is a DataRequest
,它继承自Request
它有一个相当大的覆盖debugDescription
那个叫curlRepresentation().
如果您打印request
,你将拥有:
$> CredStore - performQuery - Error copying matching creds. Error=-25300, query={
atyp = http;
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = http;
"r_Attributes" = 1;
sdmn = someUrl;
srvr = someUrl;
sync = syna;
}
$ curl -v \
-X DELETE \
-H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \
-H "User-Agent: iOSTest/1.0 (nt.iOSTest; build:1; iOS 11.4.0) Alamofire/4.7.3" \
-H "Accept-Language: en;q=1.0, fr-FR;q=0.9" \
"http://someUrl?id=someId"
很酷,对吧?但不是-d
选项。你甚至可以检查它print(request.request.httpBody)
并得到:
$> nil
要修复它,请使用encoding
(ParameterEncoding
) init. 中的参数默认使用即可JSONEncoding
, URLEncoding
and PropertyListEncoding
.
但你想把参数放在httpBody
,所以使用URLEncoding(destination: .httpBody)
:
Alamofire.request("http://someUrl", method: .delete, parameters: params, encoding: URLEncoding(destination: .httpBody), headers: headers)
你会得到:
$>CredStore - performQuery - Error copying matching creds. Error=-25300, query={
atyp = http;
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = http;
"r_Attributes" = 1;
sdmn = someUrl;
srvr = someUrl;
sync = syna;
}
$ curl -v \
-X DELETE \
-H "Authorization: JWT someToken" \
-H "User-Agent: iOSTest/1.0 (nt.iOSTest; build:1; iOS 11.4.0) Alamofire/4.7.3" \
-H "Accept-Language: en;q=1.0, fr-FR;q=0.9" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \
-d "id=someId" \
"http://someUrl"