简体   繁体   English

使用 RestAssured 时收到 400 错误请求错误

[英]Getting a 400 bad request error when using RestAssured

I am getting a 400 error on a POST request in InteliJ/Java/RestAssured but not in Postman, so can anyone advise where I am getting it wrong please First Postman我在 InteliJ/Java/RestAssured 中的 POST 请求中收到 400 错误,但在 Postman 中没有,所以任何人都可以告诉我哪里弄错了请首先 Postman

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

在此处输入图像描述

And my body params are我的身体参数是

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

Now when I post this I get a 200 response and a nice new access token.现在,当我发布此消息时,我会收到 200 响应和一个不错的新访问令牌。 When I try the same in Java/RestAssured I get a 400 bad request error, this is what I post.当我在 Java/RestAssured 中尝试相同的操作时,我收到 400 bad request 错误,这就是我发布的内容。

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"

Every time I run this I get HTTP/1.1 400 Bad Request error, I cannot figure out why.每次我运行这个我得到 HTTP/1.1 400 Bad Request 错误,我不知道为什么。 Can anyone see where I am going wrong please.谁能看到我要去哪里错了。 Thanks in advance.提前致谢。

Below are the returned 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

And cookies和 cookies

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

Well, I did some hunting around and managed to sort a solution, as below好吧,我四处寻找并设法解决了一个解决方案,如下所示

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"));

Live and learn活到老,学到老

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM