0
我新的iOS開發500錯誤。 GET方法正常工作。HTTP POST方法返回使用SWIFT的蔚藍網絡API服務器
但是,POST方法將返回500響應..
這是帖子控制器斯威夫特:
@IBAction func pressedPost(_ sender: Any) {
let restEndPoinst: String = "http://tresmorewebapi.azurewebsites.net/api/accounts"
guard let url = URL(string: restEndPoinst) else {
print("Error creating URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
// api key need urlRequest.setValue(<#T##value: String?##String?#>, forHTTPHeaderField: "APIKey")
let jsonDictionary = NSMutableDictionary()
jsonDictionary.setValue(9, forKey: "Id")
jsonDictionary.setValue("From iOS!", forKey: "UserName")
jsonDictionary.setValue("HAHAH iOS", forKey: "UserEmail")
jsonDictionary.setValue(200.44, forKey: "Rebate")
jsonDictionary.setValue(1200.20, forKey: "MemCom")
let jsonData: Data
do{
jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: JSONSerialization.WritingOptions())
}
catch{
print("Error creating JSON")
return
}
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest, completionHandler:
{
(data: Data?, response: URLResponse?, error: Error?) in
print("Error:")
print(error)
print("response:")
print(response)
print("Data:")
print(String(data: data!, encoding: String.Encoding.utf8))
})
task.resume()
}
我的模型看起來象下面這樣:
public class Account
{
public int Id { get; set; }
public string UserName { get; set; }
public string UserEmail { get; set; }
public decimal Rebate { get; set; }
public decimal MemCom { get; set; }
}
我,如果你搜索iOS Swift Calling POST, PUT and DELETE on Azure REST Web Service
這是我繼一個利用asp.net網頁API 迅速和蔚藍隨後的Youtube教程。
請幫我如何解決POST請求的一些數據插入到我的蔚藍的Web服務器。
根據定義,500 HTTP狀態代碼是一個內部服務器錯誤。如果您的請求出現問題(例如,格式錯誤的有效負載),則應該收到4xx狀態碼,例如, 400或403.這似乎是服務器的問題。 – daltonclaybrook
@daltonclaybrook不過,如果我使用HTTP郵差調試程序,我可以做POST和它完美的作品!但我不知道爲什麼的iOS提供了500錯誤 –
其實,是有可能,你忘序列化JSON數據後,你的URLRequest的'httpBody'屬性分配? – daltonclaybrook