Json не отправлять через Unirest.post - PullRequest
0 голосов
/ 10 октября 2019

Я пытаюсь отправить объект Json через Unirest.post () внутри класса Test, у меня есть работающее соединение с URL, но получено исключение, что не было передано объекта Json. Включен код вызова API ниже:

 try{
        String location = "http://localhost:" + port + "/check-person";
        System.out.println(location);
        HttpResponse<JsonNode> jsonResponse =
        Unirest.post("http://localhost:" + port + "/check-person")
        .body("{\"personName\": \"Darth Vader\"}")
        .asJson();
        assertEquals(403, jsonResponse.getStatus());
    } catch(UnirestException e){
        e.printStackTrace();
        System.out.println(e);
    }

1 Ответ

0 голосов
/ 11 октября 2019

Включить Content-Type в заголовок как JSON. Это скажет серверу, что тело должно быть интерпретировано как JSON. Некоторые распространенные типы:

«application / json», «application / xml», «text / html», «text / plain»

Добавить тип содержимогозаголовок в запросе UniRest как в

try{
    String location = "http://localhost:" + port + "/check-person";
    System.out.println(location);
    HttpResponse<JsonNode> jsonResponse =
    Unirest.post("http://localhost:" + port + "/check-person")
    .header("Content-Type", "application/json")
    .body("{\"personName\": \"Darth Vader\"}")
    .asJson();
    assertEquals(403, jsonResponse.getStatus());
} catch(UnirestException e){
    e.printStackTrace();
    System.out.println(e);
}
...