簡體   English   中英

Restassured 測試錯誤 - No Content-Type was specified in response

[英]Restassured test error - No Content-Type was specified in response

我嘗試放心地測試我的休息端點。 響應始終是一個 html 文檔,盡管我將 accept-header 設置為“application/json”。

*java.lang.IllegalStateException:無法解析對象,因為在響應中沒有指定支持的內容類型。 內容類型為“text/html;charset=utf-8”。

[main] DEBUG org.apache.http.wire - << "HTTP 狀態 400 [0xe2][0x80][0x93] 錯誤請求*

編輯:在郵遞員中,它處理相同的請求。

myClass result = given()
                    .contentType("application/json")
                    .accept("application/json")
                    .header("sessionid", sessionId)
                    .body(myBody)
                    .when()
                    .post(getInternalEndpoint() + "/rest/v1/myEndpoint").as(myClass.class);

我認為您可能沒有正確發送請求。 我使用 Rest Assured 在 Java 中編寫了以下代碼:

RequestSpecification httprequest = RestAssured.given();
//request Payload
JSONObject js =new JSONObject();
js.put("name","xyz");

//Add a header stating that request body is a JSON
httprequest.header("Content-Type","application/json");
httprequest.body(js.toJSONString());
httprequest.header("Authorization","Bearer yourAPIKEy");
httprequest.log().all();

//Response
Response response = httprequest.request(Method.POST,"/rest/v1/myEndpoint");
String responseBody = response.asString();

//JsonPath to read response body
JsonPath json=response.jsonPath();
String statusCode = json.get("statusCode)).toString();
System.out.println(statusCode);

暫無
暫無

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

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