2

The server that I am using returns error messages in the HTTP status message. For example, it will return "400 User already exists" rather than "400 Bad Request".

I would like to access the string "User already exists" in the response method called by Alamofire. However, I cannot find any way to access this string.

I found this question on StackOverflow already: Swift Alamofire: How to get the HTTP response status code

Unfortunately, no one gives an answer to the question. :(

Here is where Chrome shows where the error is:

Status message

Community
  • 1
  • 1
thedayturns
  • 9,723
  • 5
  • 33
  • 41
  • You can access the HTTP response code itself by using the `statusCode` property of the response object in the Alamofire callback. This is an `Int` though, not a `String`. If you're certain that error string is being sent back, then I'd advise using something like [POSTman](https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en) to check the headers and body to find out where. This will help you work out how to access it. – matthew.healy May 08 '15 at 23:28
  • @matthew.healy: I uploaded an image showing the status message in Chrome. I don't believe that's part of the headers or body, though I certainly could be wrong. – thedayturns May 08 '15 at 23:40
  • Ah, very strange. Not sure there's much I can do to help really. It seems the only things you can get from the response are the (integer) `statusCode` and a dictionary of headers. (See [this link](https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPURLResponse_Class/index.html)) – matthew.healy May 08 '15 at 23:53
  • I also have the same requirement. Did you find an answer for this? – Oshadha Sep 24 '15 at 21:45
  • @Oshadha Yes, although you're not going to like it. I learned that the HTTP status message is supposed to be for debugging only and is not intended to be used in app logic. Any meaningful data should be sent via header or body. Therefore you should never need to get the status message, and if you do need it, you should change your server. (Again, I said you wouldn't like my answer! :P) – thedayturns Sep 25 '15 at 04:49
  • @thedayturns thanks for the answer. I will try to get this done by server guys. – Oshadha Sep 25 '15 at 17:06

2 Answers2

3

I would suggest trying to print out all the possible data fields that you are given and see what you can find. Please try the following example and see if that sheds any light.

let URL = NSURL(string: "your/url/to/somewhere")!
let parameters = ["foo": "bar"]

Alamofire.request(.POST, URL, parameters: parameters)
    .response { request, response, data, error in
        println("Request: \(request)")
        println("Response: \(response)")
        println("Error: \(error)")

        if let data = data as? NSData {
            println("Data: \(NSString(data: data, encoding: NSUTF8StringEncoding)!)")
        }
}
cnoon
  • 16,575
  • 7
  • 58
  • 66
0

Return response in json format from the server and then i think you'll be able to get the appropriate status.

I've implemented that thing using php codeigniter..from where my response is like

 $response['status'] = 'user_already_exists'; 
 $this->response($response, 400);

Now in swift you can go with this

Alamofire.request(.POST,URL, parameters:parameters) .responseJSON
{
            (request, response, data, error) in

            var json = JSON(data!) //I've used swiftyJSON for reading json response
            let status = json["status"].stringValue
            println("Status : \(status)")
}

Hope this may help you.

iRiziya
  • 3,235
  • 1
  • 22
  • 36