Alamofire raw json string to send or post

How can I send json source string from put or post method using Alamofire?

I can not find any example for this.

let params = Mapper().toJSONString(results) // json string with array of objects Alamofire.request(.PUT, Config().apiGroup, parameters: params) 

error:

 Cannot convert value of type 'String?' to expected argument type '[String : AnyObject]?' 
+5
source share
1 answer

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

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

See this example in the Alamofire Document :

 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) } 

Hope this helps you.

+4
source

Source: https://habr.com/ru/post/1235157/


All Articles