Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]?'
like image 273
Mirza Delic Avatar asked Oct 19 '22 23:10

Mirza Delic


1 Answers

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.

like image 113
Victor Sigler Avatar answered Nov 18 '22 21:11

Victor Sigler