使用 RestAssured 时出现 400 错误请求错误

Getting a 400 bad request error when using RestAssured

我在 InteliJ/Java/RestAssured 中的 POST 请求中收到 400 错误,但在 Postman 中却没有,所以谁能告诉我哪里出错了 第一邮递员

Endpoint: https://xxxxxx.auth.us-east-2.amazoncognito.com/oauth2/token

我的 body 参数是

client_id       anvalidid
client_secret   shhhhitisasecret    
scope           https://xxxxxx.auth.us-east-2.amazoncognito.com/read    
grant_type      client_credentials

现在,当我 post 收到 200 响应和一个不错的新访问令牌时。 当我在 Java/RestAssured 中尝试相同时,我收到 400 错误请求错误,这就是我 post.

Endpoint: https://xxxxxx.auth.us-east-2.amazoncognito.com/oauth2/token

Body

{
"client_id":"anvalidid",
"client_secret":"shhhhitisasecret",
"scope":"https://xxxxxx.auth.us-east-2.amazoncognito.com/read",
"grant_type":"client_credentials"
}

Header: "Content-Type", "application/x-www-form-urlencoded"

每次我 运行 我得到 HTTP/1.1 400 Bad Request 错误,我不知道为什么。 谁能看到我哪里出错了。 提前致谢。

下面是返回的headers

Date=Thu, 03 Jun 2021 10:45:55 GMT
Content-Type=application/json;charset=UTF-8
Transfer-Encoding=chunked
Connection=keep-alive
Set-Cookie=XSRF-TOKEN=093edd16-255e-42ad-9f11-be84cd56c5dc; Path=/; Secure; HttpOnly; SameSite=Lax
x-amz-cognito-request-id=4c91be06-9d0b-47d9-997e-f292eee61650
X-Application-Context=application:prod:8443
X-Content-Type-Options=nosniff
X-XSS-Protection=1; mode=block
Cache-Control=no-cache, no-store, max-age=0, must-revalidate
Pragma=no-cache
Expires=0
Strict-Transport-Security=max-age=31536000 ; includeSubDomains
X-Frame-Options=DENY
Server=Server

还有饼干

XSRF-TOKEN=093edd16-255e-42ad-9f11-be84cd56c5dc;Path=/;Secure;HttpOnly

好吧,我四处寻找并设法整理了一个解决方案,如下所示

Response response = RestAssured
        .given()
        .header("Content-Type", "application/x-www-form-urlencoded")
        .formParam("client_id", "clientId")
        .formParam("client_secret", "clientSecret")
        .formParam("scope", "https://scope.etc")
        .formParam("grant_type", "client_credentials")
        .request()
        .post(settingsRepository.getSetting("cognito.url.base"));

生活和学习