Set content-type when performing GET using Alamofire 4

半城伤御伤魂 提交于 2020-01-07 08:27:12

问题


All I want to do is make a simple GET request using Alamofire 4 but set the content-type to application/json

I cannot see a way of doing it on the request. How do I do this.

I thought this would work but it doesn't.

Alamofire.request("https://dummy.com/dummy",
                           method: .get,
                           parameters: nil,
                           encoding: JSONEncoding.default,
                           headers: nil).responseJSON {
            response in
            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response 
}

I need to do this so that the service I am calling will return the data as JSON rather than XML. Any ideas?


回答1:


Set headers for that, create dictionary and set it with headers with Alamofire request.

let header = ["content-type" : "application/json"]

Alamofire.request("https://dummy.com/dummy",
                       method: .get,
                       parameters: nil,
                       encoding: JSONEncoding.default,
                       headers: header).responseJSON {
        response in
        print(response.request)  // original URL request
        print(response.response) // HTTP URL response
        print(response.data)     // server data
        print(response.result)   // result of response 
}


来源:https://stackoverflow.com/questions/42486243/set-content-type-when-performing-get-using-alamofire-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!