Swift 带-d的阿拉莫菲尔
我需要像这个邮递员一样提出请求,但是在阿拉莫菲尔Swift 带-d的阿拉莫菲尔,swift,curl,alamofire,Swift,Curl,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' \
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
我想应该是这样的:
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)
}
}
我如何从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
有一个很好的覆盖
如果打印请求
,您将有:
$> 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
要解决此问题,请使用init中的编码
(参数编码
)参数。默认情况下,您可以使用JSONEncoding
、URLEncoding
和PropertyListEncoding
但是您希望将参数放入httpBody
,因此请使用URLEncoding(目的地:.httpBody)
:
您将得到:
$>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"
您可以这样做:
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
有一个很好的覆盖
如果打印请求
,您将有:
$> 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
要解决此问题,请使用init中的编码
(参数编码
)参数。默认情况下,您可以使用JSONEncoding
、URLEncoding
和PropertyListEncoding
但是您希望将参数放入httpBody
,因此请使用URLEncoding(目的地:.httpBody)
:
您将得到:
$>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"
let request=Alamofire.request(“http://someUrl,方法:。删除,参数:参数,标题:标题);改为request.validate().responseJSON{…}
。打印request
,您也可以打印request.request.httpBody
,并查看您的案例中是否没有httpBody。如果使用(方法…编码:JSONEncoding()…)
,您将看到-d
选项,但改用JSON编码(-d“{\”id\:\”someId\“}”
)。要使其工作,请使用Alamofire.request(“http://someUrl,方法:.delete,参数:params,编码:URLEncoding(目标:.httpBody),头:头)
let request=Alamofire.request('http://someUrl,方法:。删除,参数:参数,标题:标题);改为request.validate().responseJSON{…}
。打印request
,您也可以打印request.request.httpBody
,并查看您的案例中是否没有httpBody。如果使用(方法…编码:JSONEncoding()…)
,您将看到-d
选项,但改用JSON编码(-d“{\”id\:\”someId\“}”
)。要使其工作,请使用Alamofire.request(“http://someUrl,方法:.delete,参数:params,编码:URLEncoding(目标:.httpBody),头:头)
。非常感谢您的解释!对于请求的debugDescription
,现在我可以看到请求中发生了什么),请您做一些解释-如何在Alamofire请求中传递-f
标志?这是为了防止日志出错,不是吗?那就不要读错误了?不,没有错误,你的答案也行!我只是想知道,如果我的问题是正确的,我应该在您的代码中更改什么以根据文档传递参数-f
-f
或--fail
表示“(HTTP)在服务器错误上以静默方式失败(完全没有输出)。这样做主要是为了更好地启用脚本等以更好地处理失败的尝试。在正常情况下,当HTTP服务器无法交付文档时,它会返回一个HTML文档,说明原因(通常还描述了原因及更多)。此标志将防止curl输出该值并返回错误22。此方法不是故障安全的,并且有时会漏掉不成功的响应代码,特别是在涉及身份验证时(响应代码401和407)。“非常感谢您的解释!对于请求的debugDescription
,现在我可以看到请求中发生了什么),请您做一些解释-如何在Alamofire请求中传递-f
标志?这是为了防止日志出错,不是吗?那就不要读错误了?不,没有错误,你的答案也行!我只是想知道,如果我的问题是正确的,我应该在您的代码中更改什么以根据文档传递参数-f
-f
或--fail
表示“(HTTP)在服务器错误上以静默方式失败(完全没有输出)。这样做主要是为了更好地启用脚本等以更好地处理失败的尝试。在正常情况下,当HTTP服务器无法交付文档时,它会返回一个HTML文档,说明原因(通常还描述了原因及更多)。此标志将防止curl输出该值并返回错误22。此方法不是故障安全的,并且有时会漏掉不成功的响应代码,特别是在涉及身份验证时(响应代码401和407)。”