我正试图通过IOS Swift应用程序访问JHipster生成的API,它是我在Heroku上上传的。JHipster应用程序由OAuth2保护。基本上,我想实现一个注册功能,在IOS应用程序中提供凭据(电子邮件、登录名和密码),然后调用JHipster应用程序上的/api/register路径来注册这个用户。但当我发出一个Swift Alamofire请求时,当我试图调用/api/register时,它会给我一个500的状态码。
我在Heroku的JHipster应用程序:
https://j-hipster-test-apiios.herokuapp.com
我的Swift Alamofire请求:

class RegisterViewController: UIViewController {

let parameters: Parameters = [
    "login": "Hello",
    "password": "World",
    "email": "[email protected]"
 ]


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

}

我的Xcode日志(responseValidationFailed错误是由请求后的.validate()方法引起的,因为它期望状态代码介于200和299之间):
 FAILURE:
         responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500))

我的英雄日志:
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

所以主要的问题是,为什么我在调用JHipster api的/api/register路径时会得到一个500状态码?
提前谢谢!

最佳答案

我终于解决了这个问题。这不是JHipster问题,而是Alamofire解析问题。我总是使用无效的json数据发出请求,因为我没有在Swift代码中验证凭证字典。如果我用JSONSerialization.data(withJSONObject: parameters)序列化json数据,我可以注册一个帐户。为了完整起见,以下是我的解决方案:

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": "[email protected]"
        "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()
    }

  }

}

关于ios - 通过Swift Alamofire访问JHipster Heroku Oauth2 App上的500 Internal Server Error,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40429124/

10-11 17:37