带-d的Alamofire [英] Alamofire with -d

查看:78
本文介绍了带-d的Alamofire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像邮递员一样发出请求,但是在Alamofire中

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

我想应该是这样的:

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

How can I check that my request has option like this from cUrl - -d id=someId

推荐答案

您执行以下操作:

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 DataRequest ,它继承自 Request ,具有相当大的覆盖的 debugDescription 调用 curlRepresentation()

request is a DataRequest, which inherits from Request which has a pretty override of debugDescription that calls curlRepresentation().

如果打印请求,则将具有:

$> 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)进行检查,并获得:

Pretty cool, right? But no -d option. You can even check it with print(request.request.httpBody) and get:

$> nil

要解决此问题,请使用编码 init中的( ParameterEncoding )参数。默认情况下,可以使用 JSONEncoding URLEncoding PropertyListEncoding

To fix it, use the encoding (ParameterEncoding) parameter in the init. You can use by default JSONEncoding, URLEncoding and PropertyListEncoding.

但是您要将参数放在 httpBody 中,因此请使用 URLEncoding(destination :.httpBody)

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)

您会得到:

$>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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆