Tobias Mini Tobias Mini - 1 month ago 36
iOS Question

500 Internal Server Error on JHipster Heroku Oauth2 App when accessing via Swift Alamofire

I am trying to access a JHipster generated API, which i uploaded on Heroku, via an IOS Swift app. The JHipster app is secured with OAuth2. Basically i want to implement a register functionality where i provide the credentials (email, login and password) in the IOS app and then call the

/api/register
path on the JHipster app to register this user. But when i make a Swift Alamofire request it gives me a status code of 500 when trying to call
/api/register
.

My JHipster app at Heroku:
https://j-hipster-test-apiios.herokuapp.com

My Swift Alamofire request:

class RegisterViewController: UIViewController {

let parameters: Parameters = [
"login": "Hello",
"password": "World",
"email": "Test@googlemail.com"
]


@IBAction func register(_ sender: UIButton) {

let sessionManager = SessionManager()

sessionManager.request("https://j-hipster-test-apiios.herokuapp.com/api/register", method: .post, parameters: parameters).validate().responseJSON { response in


print("----------------------------")
print(response)
print("----------------------------")

sessionManager.session.invalidateAndCancel()
}

}


My Xcode log (The responseValidationFailed error is caused by the .validate() method after the request, because it expects a status code between 200 and 299):

FAILURE:
responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500))


My Heroku log:

2016-11-04T17:38:36.148902+00:00 heroku[router]: at=info method=POST path="/api/register"
host=j-hipster-test-apiios.herokuapp.com request_id=7a1e1345-a8a5-43f2-abbc-4805037a47ef
fwd="93.210.40.139" dyno=web.1 connect=0ms service=13ms status=500 bytes=317


So the main question is, why i get a 500 status code when i call the
/api/register
path of my JHipster api?

Thanks in advance!

Answer

Finally i solved the problem. This was not a JHipster problem but an Alamofire parsing problem. I always made requests with invalid json data because i didn´t validate my credentials Dictionary in my Swift code. If i serialize my json data with JSONSerialization.data(withJSONObject: parameters) i can register a account. For completeness here´s my solution:

class RegisterViewController: UIViewController {

@IBAction func register(_ sender: UIButton) {

    let url = NSURL(string: "https://j-hipster-test-apiios.herokuapp.com/api/register")

    var request = URLRequest(url: url as! URL)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    let parameters: Parameters = [

        "activated": true,
        "authorities": [  "ROLE_USER"  ],
        "createdBy": "TestName",
        "createdDate": "2016-11-04T20:42:20.301Z",
        "login": "Hello",
        "password": "World",
        "email": "Test@googlemail.com"
        "firstName": "TestName",
        "id": 0,  
        "langKey": "en",
        "lastModifiedBy": "TestName",
        "lastModifiedDate": "2016-11-04T20:42:20.302Z",
        "lastName": "TestName",


    ]

    request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)


    let sessionManager = SessionManager()
    //self.alamofireManager = sessionManager


    sessionManager.request(request).responseJSON { response in

        switch response.response!.statusCode {
        case 201: print("Created")
        case 400: print("Username already in use")
        case 500: print("Error!")
        default: print("Error!")
        }

        sessionManager.session.invalidateAndCancel()
    }

  }

}