0

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.

    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!

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
dashbashrumble
  • 251
  • 1
  • 5
  • 19
  • just a sidenote, why don't u use the native task that comes with iOS. it is really powerful. – William Kinaan Oct 06 '15 at 11:09
  • I haven't heard of such a thing, please elaborate. – dashbashrumble Oct 06 '15 at 11:11
  • please check for example [this question](http://stackoverflow.com/questions/32953501/cannot-convert-jsonarray-element-to-integer/32954028#32954028) about sending a post request and parse the response json. [Another question](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) – William Kinaan Oct 06 '15 at 11:12
  • HTTP 400 = "Bad Request". Your `productJSON` parameters are likely to be wrong. – Eric Aya Oct 06 '15 at 11:20
  • Also check NSAppTransportSecurity if you are using HTTP, and not HTTPS http://stackoverflow.com/questions/31216758/how-can-i-add-nsapptransportsecurity-to-my-info-plist-file – KrisJones Oct 06 '15 at 12:28

2 Answers2

0

You are getting http error 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.

Miknash
  • 7,888
  • 3
  • 34
  • 46
0

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

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)
cnoon
  • 16,575
  • 7
  • 58
  • 66