2015-10-06 80 views
0

我是Alamofire和Swift的新手,請耐心等待。我正嘗試使用Alamofire將產品JSON上傳到服務器。但是我不斷收到狀態碼:400失敗。有沒有辦法記錄發送到服務器的請求,以便我可以檢查我的數據格式是否正確。我在下面提供了上傳代碼。如何登錄HTTP POST請求

static func uploadProduct(productJSON: [String : AnyObject]) { 
    Alamofire.request(.POST, ServerConfig.ADD_PRODUCT_URL, parameters: productJSON, encoding: .JSON, headers: nil) 
     .responseJSON(completionHandler: { responseRequest, responseResponse, responseResult in 
      print(responseRequest!.URL) 
      print(responseResponse) 
      print(responseResult) 
    }) 
} 

這是我得到的錯誤。

sellers.strawmine.com/api/v1/products/add } { status code: 400, headers { 
Connection = "keep-alive"; 
Date = "Tue, 06 Oct 2015 10:53:44 GMT"; 
Server = "nginx/1.4.6 (Ubuntu)"; 
"Transfer-Encoding" = Identity; 
} }) 
FAILURE 

請幫忙。我在這方面花了很多時間。提前致謝!

+0

只是一個旁註,爲什麼不使用iOS自帶的本地任務。它非常強大。 –

+0

我還沒有聽說過這樣的事情,請詳細說明。 – dashbashrumble

+0

請檢查示例[這個問題](http://stackoverflow.com/questions/32953501/cannot-convert-jsonarray-element-to-integer/32954028#32954028)關於發送一個post請求並解析響應json。 [另一個問題](http://stackoverflow.com/questions/32951490/sendsynchronousrequest-is-deprecated-in-ios-9/32951711#32951711),[Another quesiton](http://stackoverflow.com/questions/32786566/sending-custom-http-headers-in-swift/32786812#32786812) –

回答

0

你得到HTTP錯誤400:http://www.checkupdown.com/status/E400.html

或者乾脆,錯誤的請求。

我想你應該檢查你的端點是否正確,以及你的服務器是否配置了該動作的路由。不確定你的後端是什麼。

0

根據CustomDebugStringConvertible協議,您應該利用內置於Request對象中的cURL支持。

let request = Alamofire.request(.POST, ServerConfig.ADD_PRODUCT_URL, parameters: productJSON, encoding: .JSON) 
request.responseJSON { request, response, result in 
     print(request!.URL) 
     print(response) 
     print(result) 
    } 

debugPrint(request)