如何记录HTTP POST请求

时间:2022-09-07 11:16:35

I am new to Alamofire and Swift, so please bear with me. I am trying to upload a Product JSON to server using Alamofire. However I keep getting a status code : 400 failure. Is there any way to log the request being sent to the server so that I can check if my data format is proper. I have provided the code for upload below.

我是Alamofire和Swift的新手,所以请耐心等待。我正在尝试使用Alamofire将Product JSON上传到服务器。但是我一直收到状态码:400失败。有没有办法记录发送到服务器的请求,以便我可以检查我的数据格式是否正确。我在下面提供了上传代码。

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

This is the error that I get.

这是我得到的错误。

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

Please help. I have spent a good amount of time on this. Thanks in advance!

请帮忙。我花了很多时间在这上面。提前致谢!

2 个解决方案

#1


0  

You are getting http error 400 : http://www.checkupdown.com/status/E400.html

您收到http错误400:http://www.checkupdown.com/status/E400.html

Or simply, bad request.

或简单地说,糟糕的要求。

I think you should check if your endpoint is correct, and if your server has configured route for that action. Not sure what your backend is.

我认为您应该检查您的端点是否正确,以及您的服务器是否已为该操作配置了路由。不确定你的后端是什么。

#2


0  

You should leverage the cURL support built into the Request object by conforming to the CustomDebugStringConvertible protocol.

您应该通过符合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)

#1


0  

You are getting http error 400 : http://www.checkupdown.com/status/E400.html

您收到http错误400:http://www.checkupdown.com/status/E400.html

Or simply, bad request.

或简单地说,糟糕的要求。

I think you should check if your endpoint is correct, and if your server has configured route for that action. Not sure what your backend is.

我认为您应该检查您的端点是否正确,以及您的服务器是否已为该操作配置了路由。不确定你的后端是什么。

#2


0  

You should leverage the cURL support built into the Request object by conforming to the CustomDebugStringConvertible protocol.

您应该通过符合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)