[英]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.