0

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

nastassia
  • 807
  • 1
  • 12
  • 31
  • `let request = Alamofire.request("http://someUrl", method: .delete, parameters: params, headers: headers); request.validate().responseJSON{...}` Instead. Print `request`, you can also print `request.request.httpBody` and see that there is no HTTPBody in your case. – Larme Dec 05 '18 at 17:21
  • 1
    If you use `(method... encoding: JSONEncoding()...)`, you'll see the `-d` option, but with JSON encoding instead (`-d "{\"id\":\"someId\"}"`). To make it work, use `Alamofire.request("http://someUrl", method: .delete, parameters: params, encoding: URLEncoding(destination: .httpBody), headers: headers)`. – Larme Dec 05 '18 at 17:27

1 Answers1

4

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"
Larme
  • 24,190
  • 6
  • 51
  • 81
  • thanks a lot for explanation! and for `debugDescription` of the `request`, now I can see what's going inside the request) – nastassia Dec 05 '18 at 17:49
  • and please can you make some more explanation - how to pass `-f` flag at Alamofire request? – nastassia Dec 05 '18 at 18:30
  • That’s to prevent the logs in case of error, no ? Don’t read the error then? – Larme Dec 05 '18 at 18:42
  • no,there is no error, your answer works as well! just want to know what should I change in your code to pass `params` as `-f`, if my question is correct) – nastassia Dec 05 '18 at 18:45
  • According to the doc. `-f` or `--fail` is for "(HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22. This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407)." – Larme Dec 05 '18 at 18:52
  • If I understood correctly, it's just for output in "Terminal" (or in your script if you read the return value). So it's like "I can know it failed because it returned 22, but I won't show it", except in case of 401 and 407 HTTP Code apparently. So that's not really a `cURL` param that will affect the request, it's more on the "app" part. – Larme Dec 05 '18 at 18:53
  • oh my bad, I guess I should asked about `-F`, not `-f`. To pass form data. What should be headers in this case and common request form? – nastassia Dec 05 '18 at 19:05
  • Aah... That's for `multipart/form-data` Alamofire has a method for, you can look there: https://stackoverflow.com/questions/31949118/send-post-parameters-with-multipartformdata-using-alamofire-in-ios-swift – Larme Dec 05 '18 at 19:09