带有 -d 的 Alamofire [英] Alamofire with -d
问题描述
我需要像邮递员那样提出请求,但在 Alamofire 中
curl -X DELETE http://someUrl -H '授权:JWT someToken' -H '缓存控制:无缓存' -H '内容类型:应用程序/x-www-form-urlencoded' -H '邮递员令牌:0149ef1e-5d45-34ce-3344-4b658f01bd64' -d id=someId
我想应该是这样的:
let headers = ["Content-Type": "application/x-www-form-urlencoded", "Authorization": "JWT someToken"]让参数:[字符串:任何] = [id":someId"]Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers).validate().responseJSON { responseJSON in切换 responseJSON.result {案例.成功(_):让数据 = responseJSON.result.value!打印(数据)案例.失败(让错误):打印(错误.本地化描述)}}
如何从 cUrl
- -d id=someId
你这样做:
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
是一个 DataRequest
,它继承自 Request
,它具有调用 debugDescription
的漂亮覆盖a href="https://github.com/Alamofire/Alamofire/blob/d0208adc369b3fb1e622aca5f0a3e0d170c4832b/Source/Request.swift" rel="nofollow noreferrer">curlRepresentation()
.>
如果你打印request
,你会得到:
$>CredStore - performQuery - 复制匹配的凭据时出错.错误=-25300,查询={atyp = http;类 = inet;"m_Limit" = "m_LimitAll";ptcl = http;"r_Attributes" = 1;sdmn = someUrl;srvr = someUrl;同步 = 同步;}$ curl -v -X 删除 -H "接受编码: 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 "接受语言:en;q=1.0, fr-FR;q=0.9" http://someUrl?id=someId"
很酷吧?但是没有 -d
选项.你甚至可以用 print(request.request.httpBody)
检查它并得到:
$>零
要修复它,请在 init.d 文件中使用 encoding
(ParameterEncoding
) 参数.您可以默认使用 JSONEncoding
、URLEncoding
和 PropertyListEncoding
.
但是你想把参数放在httpBody
中,所以使用URLEncoding(destination: .httpBody)
:
Alamofire.request("http://someUrl", method: .delete, 参数: params, encoding: URLEncoding(destination: .httpBody), headers: headers)
你会得到:
$>CredStore - performQuery - 复制匹配的凭据时出错.错误=-25300,查询={atyp = http;类 = inet;"m_Limit" = "m_LimitAll";ptcl = http;"r_Attributes" = 1;sdmn = someUrl;srvr = someUrl;同步 = 同步;}$ curl -v -X 删除 -H "授权:JWT someToken" -H "User-Agent: iOSTest/1.0 (nt.iOSTest; build:1; iOS 11.4.0) Alamofire/4.7.3" -H "接受语言:en;q=1.0, fr-FR;q=0.9" -H "内容类型:应用程序/x-www-form-urlencoded" -H "接受编码: gzip;q=1.0, compress;q=0.5" -d "id=someId" http://someUrl"
I need to make request like this Postman one, but in Alamofire
curl -X DELETE
http://someUrl
-H 'authorization: JWT someToken'
-H 'cache-control: no-cache'
-H 'content-type: application/x-www-form-urlencoded'
-H 'postman-token: 0149ef1e-5d45-34ce-3344-4b658f01bd64'
-d id=someId
I guess it should be something like:
let headers = ["Content-Type": "application/x-www-form-urlencoded", "Authorization": "JWT someToken"]
let params: [String: Any] = ["id": "someId"]
Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers).validate().responseJSON { responseJSON in
switch responseJSON.result {
case .success( _):
let data = responseJSON.result.value!
print(data)
case .failure(let error):
print(error.localizedDescription)
}
}
How can I check that my request has option like this from cUrl
- -d id=someId
You do this:
Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers).validate().responseJSON { ... }
In fact, it can be deconstruct like that:
let request = Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers)
request.validate().responseJSON { ... }
request
is a DataRequest
, which inherits from Request
which has a pretty override of debugDescription
that calls curlRepresentation()
.
If you print request
, you'll have:
$> 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"
Pretty cool, right? But no -d
option. You can even check it with print(request.request.httpBody)
and get:
$> nil
To fix it, use the encoding
(ParameterEncoding
) parameter in the init. You can use by default JSONEncoding
, URLEncoding
and PropertyListEncoding
.
But you want to put the parameter in the httpBody
, so use URLEncoding(destination: .httpBody)
:
Alamofire.request("http://someUrl", method: .delete, parameters: params, encoding: URLEncoding(destination: .httpBody), headers: headers)
And you'll get:
$>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"
这篇关于带有 -d 的 Alamofire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!