【问题标题】:Alamofire with -d带 -d 的 Alamofire
【发布时间】:2019-05-07 07:48:57
【问题描述】:

我需要像这个邮递员一样提出请求,但在 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

我想应该是这样的:

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 的类似选项

【问题讨论】:

  • let request = Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers); request.validate().responseJSON{...} 代替。打印request,也可以打印request.request.httpBody,看看你的情况下没有HTTPBody。
  • 如果您使用 (method... encoding: JSONEncoding()...),您将看到 -d 选项,但使用 JSON 编码 (-d "{\"id\":\"someId\"}")。要使其正常工作,请使用Alamofire.request("http://someUrl", method: .delete, parameters: params, encoding: URLEncoding(destination: .httpBody), headers: headers)。

标签: swift curl alamofire


【解决方案1】:

你这样做:

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,它很好地覆盖了调用curlRepresentation() 的debugDescription。

如果您打印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

要修复它,请在 init 中使用 encoding (ParameterEncoding) 参数。您可以默认使用JSONEncoding、URLEncoding 和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"

【讨论】:

  • 非常感谢您的解释!对于request 中的debugDescription,现在我可以看到请求中发生了什么)
  • 请您再解释一下 - 如何在 Alamofire 请求中传递 -f 标志?
  • 那是为了防止日志出错,不是吗?那就不读错误了吗?
  • 不,没有错误,您的回答也有效!只是想知道我应该在您的代码中更改什么以将params 传递为-f,如果我的问题是正确的)
  • 根据文档。 -f 或 --fail 用于“(HTTP)在服务器错误时静默失败(根本没有输出)。这主要是为了更好地启用脚本等以更好地处理失败的尝试。在正常情况下,当 HTTP 服务器无法交付一个文档,它会返回一个 HTML 文档说明(通常还描述了原因等等)。此标志将阻止 curl 输出该文件并返回错误 22。此方法不是故障安全的,并且有时会出现不成功的响应代码会漏掉,尤其是在涉及身份验证时(响应代码 401 和 407)。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多