0

I am trying to make a request with form-data as in Postman, however I always get the "Parameters is invalid" response from the server. I tried with postman it works perfectly.

parameters: [String: String] = ["name": "name","email": "email"]

Here is my code:

     let request = AF
                .request(url, method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default, interceptor: self.interceptor)
                .validate()
                .responseDecodable(of: KResult<T>.self) { response in
                    switch response.result {
                    case let .success(res):
                        print(res)
                        completion(.success(res.result))
                    case let .failure(error):
                        print(error)
                        completion(.error(error))
                        }
                }

PS

I looked at this question and many others in this website but didn't solve my problem

Ahmad
  • 1
  • Little tip, you can use POSTMAN to generate `cURL` and Alamofire to generate `cURL`, might be helpful meeting each halfway and check the differences.. (I explained it there: https://stackoverflow.com/questions/53637437/alamofire-with-d/53637821#53637821 ). Since we don't know what it should be (what's the POSTMAN settings, what's the doc settings of your web service?) – Larme Sep 22 '20 at 17:46
  • thanks for your reply The doc settings for the backend it requires form-data with post command and header contains the token and the Content-Type keys I have got the output of the two but it seems messy a little bit: postman: ```curl --location --request POST 'http://example.com/api/v1/customer/profile/edit/' \ --header 'token: xxxxxxxx' \ --header 'Content-Type: application/json' \ --form 'name=test' \ --form 'email=test@test.com' \ --form 'genders=1'``` – Ahmad Sep 22 '20 at 18:45
  • here is the `request` curl: ```curl -v \ -X POST \ -H "User-Agent: Demo/1.0 (com.demo.ios; build:1; iOS 13.6.0) Alamofire/5.2.2" \ -H "Accept-Language: en;q=1.0" \ -H "Accept-Encoding: br;q=1.0, gzip;q=0.9, deflate;q=0.8" \ -H "token: xxxx" \ -H "Content-Type: application/json" \ -d "email=dsfdsfds@tes.com&gender=1&name=testdddd" \ "http://example.com/api/v1/customer/profile/edit/" ``` – Ahmad Sep 22 '20 at 18:47

1 Answers1

0

The solution was to set the Content-Type key from application/json to

application/x-www-form-urlencoded

Ahmad
  • 1