Alamofire Accept and Content-Type JSON

后端 未结 5 1080
心在旅途
心在旅途 2020-12-29 09:48

I\'m trying to make a GET request with Alamofire in Swift. I need to set the following headers:

Content-Type: application/json
Accept: application/json
         


        
5条回答
  •  一整个雨季
    2020-12-29 10:10

    Example directly from Alamofire github page:

    Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
             .validate(statusCode: 200..<300)
             .validate(contentType: ["application/json"])
             .response { (_, _, _, error) in
                      println(error)
             }
    

    In your case add what you want:

    Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
             .validate(statusCode: 200..<300)
             .validate(contentType: ["application/json"])
             .validate(Accept: ["application/json"])
             .response { (_, _, _, error) in
                      println(error)
             }
    

提交回复
热议问题