2015-10-06 79 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)