簡體   English   中英

使用“確保放心”的HTTP POST請求(示例)

[英]HTTP POST Request using Rest Assured (Example)

下面的POST方法可用於在Rest Restured中使用Java運行HTTP POST請求

與GSON依賴關系更好

{
    RestAssured.baseURI = API_URL;

    RequestSpecification request = RestAssured.given();

    request.header("Key1", "Value1");
    request.header("Key2", ""+Value2+""); //If value is getting capture from other variable

    JSONObject requestParams = new JSONObject();
    requestParams.put("Payload Key1", "Payload Value1"); 
    requestParams.put("Payload Key2", "Payload Value2");

    request.body(requestParams.toString());
    Response response = request.post(""); //last word of URL
    int StatusCode = response.getStatusCode(); //Get Status Code
    System.out.println("Status code : " + StatusCode);       
    System.out.println("Response body: " + response.body().asString()); //Get Response Body
}

可以在Rest Assured中將此代碼用於POST請求-Java

如果您正在使用Rest Assured進行API測試。 您必須將contentType指定為“ application / json”,在post方法中,您需要傳遞端點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM