簡體   English   中英

設置內容類型標頭以使用JSON和Swift 3 + AlamoFire

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

Alamofire Swift 3.0 Extra參數的回答對我來說不起作用。

將標題設置為nil編譯,但我需要["Content-Type", "application/json"] 在這里,我得到了一個額外參數的錯誤

我該怎么做

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

並發送JSON內容類型?

文檔顯示

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

我正在使用.responseJSON但我沒有收到JSON。 所以我想我需要發送Content-Type標頭。

試試這個,有另一個方法重載,允許傳遞帶標題的字典

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

在請求中發布JSON數據檢查此答案使用帶有POST的manager.request

希望這對你有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM