亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用“放心”請求 POST API 發送令牌和正文值?

如何使用“放心”請求 POST API 發送令牌和正文值?

慕田峪9158850 2022-09-28 16:08:33
我正在嘗試使用“放心”和“Java”為 POST API 創建一個測試自動化。此 POST API 具有作為應用程序/JSON 的正文,如下所示:{    "customer":{        "email": "[email protected]"    },    "password":"Teste@12"}要發出此請求,我使用以下代碼,但它返回狀態代碼“400”,但我在Postman上發送相同的信息,它返回“200”:@And("envio as informacoes da chamada: (.*), (.*), (.*), (.*) e (.*)")        public void enviarDados (String srtEmail, String srtSenha, String srtAmbiente, String srtAPI, String srtToken) {HashMap<String,String> postContent = new HashMap<String,String>();            postContent.put("email", srtEmail);            postContent.put("password", srtSenha);            //System.out.println("{\"customer\":" +postContent+ "}");            given().contentType(ContentType.JSON).header("Authorization", "Bearer"+srtToken).header("Content-Type", "application/json").            //with().body(postContent).            with().body("{\"customer\":" +postContent+ "}").            when().post(srtAmbiente+srtAPI).            then().statusCode(200); }“400”的回應是:{"status": 400,"message": "Malformed request","additional_error": ""}
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

您發送的正文與 POST 一起發送了不正確的正文。


//This line will not serialize HashMap to JSON, but call toString()

.body("{\"customer\":" +postContent+ "}")

因此,您的有效負載將如下所示:


{“客戶”:{密碼=密碼,[email protected]}}


這不是有效的 JSON。試試這個:


Map<String, String> emailContent = new HashMap<>();

emailContent.put("email", "[email protected]");

Map<String, Object> postContent = new HashMap<>();

postContent.put("customer", emailContent);

postContent.put("password", "password");

given().contentType(ContentType.JSON)

    .header("Authorization", "Bearer "+srtToken)

    .with().body(postContent)

    .when().post(srtAmbiente+srtAPI)

    .then().statusCode(200); 


查看完整回答
反對 回復 2022-09-28
  • 1 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號