quantumpotato
quantumpotato

Reputation: 9767

Setting content-type header to use JSON with Swift 3 + AlamoFire

The answers in Alamofire Swift 3.0 Extra parameter in call did not work for me.

Setting header to nil compiles but I need ["Content-Type", "application/json"]. Here I get an error of an extra parameter in th emethod

How do I take

manager.request(url, method: .get, parameters: parameters).responseJSON { response in fulfill(response) } }

and send JSON content-type?

The documentation shows

Automatic Validation

Automatically validates status code within 200..<300 range, and that the Content-Type header of the response matches the Accept header of the request, if one is provided.

Alamofire.request("https://httpbin.org/get").validate().responseJSON { response in
    switch response.result {
    case .success:
        print("Validation Successful")
    case .failure(let error):
        print(error)
    }
}

I'm using .responseJSON but I'm not getting JSON back. So I think I need to send the Content-Type header.

Upvotes: 2

Views: 5763

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

Try this, there is another method overload that allow pass a dictionary with headers

    let request = Alamofire.request(requestUrl, method: .get, parameters: [:], encoding: URLEncoding.queryString, headers: ["Content-Type" :"application/json"]).responseData { (response) in
        /***YOUR CODE***/
    }

for post JSON data in request check this answer Using manager.request with POST

Hope this helps you

Upvotes: 4

Related Questions