1

I am trying to send POST HTTP request with body using Alamofire and would appreciate any help.

My body:

{"data":{"gym":{"country":"USA","city":"San Diego","id":1}}}

Should I do something like this?

let parameters: [String: Any] = [ "data": [
  "gym": [
  "country":"USA",
  "city":"San Diego",
  "id":1
]]]

Alamofire.request(URL, method: .post, parameters: parameters, headers: headers())
  .responseJSON { response in
    print(response)
} 
Alex Schanz
  • 11
  • 1
  • 2

5 Answers5

2

If you wish to send the parameters in json format use encoding as JSONEncoding. So add parameter for encoding in request as follows:

Alamofire.request(URL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers())
   .responseJSON { response in
    print(response)
} 

Hope it helps...

Van
  • 1,225
  • 10
  • 18
0

Try this method to convert your json string to dictionary

func convertToDictionary(text: String) -> [String: Any]? {
    if let data = text.data(using: .utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
        } catch {
            print(error.localizedDescription)
        }
    }
    return nil
}

let str = "{\"data\":{\"gym\":{\"country\":\"USA\",\"city\":\"San Diego\",\"id\":1}}}"

let dict = convertToDictionary(text: str)

and send dictionary as a param in your request.

Alamofire.request(URL, method: .post, parameters: dict, headers: headers())
  .responseJSON { response in
    print(response)
} 

ref : How to convert a JSON string to a dictionary?

0

What I think is that you should try and prepare your dictionary in the this format:

var gym = [String:Any]()
gym["country"] = "USA"
gym["city"] = "San"

var data = [[String:Any]]()
data.append(gym)
var metaData = [String:Any]()
metaData["data"] = data
shravan.sukumar
  • 109
  • 2
  • 11
0

Your parameters is wrong...

let parameters: [String: Any] = { "data": 
  {
    "gym": {
      "country":"USA",
      "city":"San Diego",
      "id":1
    }
  }
}

Alamofire.request(<YOUR-URL>,
                      method: .post,
                      parameters: parameters,
                      encoding: URLEncoding(destination: .queryString),
                      headers: <YOUR-HEADER>
      ).validate().responseString { response in
      switch response.result {
      case .success:

        debugPrint("Good to go.")
        debugPrint(response)

      case .failure:

        let errMsg = String(data: response.data!, encoding: String.Encoding.utf8)!
        debugPrint(errMsg)
        debugPrint(response)

      }
    }

Hope this help. BTW, in Alamofire 5, debugPrint(response) can print out response.data directly.

ysong4
  • 131
  • 1
  • 9
0

You can use the httpBody property of URLRequest along with Alamofire Session request:

var req = try? URLRequest(url: url, method: method, headers: headers)
    req?.httpBody = someJson
Alamofire.Session(configuration: .default).request(req!).validate().response { response in 
// Handle the response 
}
zouritre
  • 231
  • 3
  • 6