Search code examples
iosjsonswiftalamofire

Alamofire raw json string to post or put


How can i send raw json string from put or post method with Alamofire?

I can't find any example for that.

let params = Mapper().toJSONString(results) // json string with array of objects

Alamofire.request(.PUT, Config().apiGroup, parameters: params)

getting error:

Cannot convert value of type 'String?' to expected argument type '[String : AnyObject]?'

Solution

  • Alamofire expect a dictionary of [String: AnyObject]? as your error said and according to your code you are trying to pass an array, you need to convert it to a dictionary instead. Check the signature of the function request in Alamofire:

    func request(method: Method, _ URLString: URLStringConvertible, 
                 parameters: [String : AnyObject]? = default, 
                 encoding: ParameterEncoding = default, 
                  headers: [String : String]? = default) -> Request
    

    See this example from the Alamofire doc:

    let params = Mapper().toJSONString(results) // json string with array of objects
    
    Alamofire.request(.PUT, "http://httpbin.org/get", parameters: ["params": params])
         .response { request, response, data, error in
             print(request)
             print(response)
             print(data)
             print(error)
          }
    

    I hope this help you.