Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

java - Unirest (Mashape) post request with binary response contains special symbols

I have difficulties executing post request that is supposed to return csv file using unirest (mashape). When I execute the request with postman I get status 200 and raw response body contains following:

enter image description here

As soon as I click on save response and download it on my local file system I do not see the symbols in the downloaded file:

"Entity Type","TOTAL ACCOUNTS" "ACC","25"

For my post request with unirest I am using .asBinary() method for the HttpResponse but it seems that the byte array contains the same symbols represented by empty space. Here is my method:

 private void checkResponseBinary(HttpResponse<InputStream> response, int expectedStatusCode) {
        Integer responseStatusCode = response.getStatus();
        InputStream responseBody = response.getBody();
        String body = "";
        try {
            body = IOUtils.toString(responseBody, StandardCharsets.UTF_8);
        } catch (IOException e) {
           e.printStackTrace();
        }

        if (responseStatusCode != expectedStatusCode) {
             throw new RuntimeException(
        "Actual response status code " + responseStatusCode + " is not equal to expected " + 
          expectedStatusCode
          + ". Body: " + responseBody);
       }
  }

At this point body String variable contains this:

enter image description here

When I write this content to file it looks even worse. The empty spaces are replaced with NUL:

enter image description here

I want the content of the csv file to look like the downloaded file from postman: "Entity Type","TOTAL ACCOUNTS" "ACC","25"

question from:https://stackoverflow.com/questions/66060982/unirest-mashape-post-request-with-binary-response-contains-special-symbols

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...