![](/img/trans.png)
[英]Getting 400 BAD Request when using Spring RestTemplate to POST
[英]Getting a 400 bad request error when using RestAssured
我在 InteliJ/Java/RestAssured 中的 POST 請求中收到 400 錯誤,但在 Postman 中沒有,所以任何人都可以告訴我哪里弄錯了請首先 Postman
Endpoint: https://xxxxxx.auth.us-east-2.amazoncognito.com/oauth2/token
我的身體參數是
client_id anvalidid
client_secret shhhhitisasecret
scope https://xxxxxx.auth.us-east-2.amazoncognito.com/read
grant_type client_credentials
現在,當我發布此消息時,我會收到 200 響應和一個不錯的新訪問令牌。 當我在 Java/RestAssured 中嘗試相同的操作時,我收到 400 bad request 錯誤,這就是我發布的內容。
Endpoint: https://xxxxxx.auth.us-east-2.amazoncognito.com/oauth2/token
身體
{
"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 錯誤,我不知道為什么。 誰能看到我要去哪里錯了。 提前致謝。
以下是返回的標題
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
和 cookies
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"));
活到老,學到老
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.