Spring RestTemplate for fetching bytes
Asked Answered
S

5

17

I am fetching the byte array using Spring Framework RestTemplate. But I also need to fetch the media type of the fetched result.

The media type of this byte array can be of any type.

The code used now for fetching bytes is below.

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.valueOf("application/pdf")));

ResponseEntity<byte[]> result = restTemp.exchange(
       url,
       HttpMethod.GET,
       entity,
       byte[].class,
       documentId
);

The above code will fetch only PDF content type.

So I have the following questions:

  • How to set the content type to accept any generic media type because the service at the other end is providing any random media type for the byte array?
  • Could someone please suggest how the media type can be fetched?

Any suggestions are welcome.

Shadoof answered 4/11, 2016 at 16:1 Comment(0)
A
22

Just not send the accept header to not force the server to return that content-type. This is the same as sending a wildcard */*

//HttpHeaders headers = new HttpHeaders();
//headers.setAccept(Collections.singletonList(MediaType.WILDCARD));
ResponseEntity<byte[]> result = restTemp.exchange(url, HttpMethod.GET, entity, byte[].class,documentId);

After this, extract the content-type from the headers of the response

 byte body[] = result.getBody();
 MediaType contentType = result.getHeaders().getContentType();
Abaxial answered 4/11, 2016 at 22:24 Comment(0)
A
1

You can set the MediaType as application/octet-stream , look at here

Aeonian answered 4/11, 2016 at 16:39 Comment(0)
H
0

You can store a media type in the HTTP headers and use - ResponseEntity.getHeaders(), for instance. or you can wrap byte array and media type into holder class.

Handsaw answered 4/11, 2016 at 16:8 Comment(1)
Thanks for the Response. Yes this will give me MediaType but only the one set in acceptHeader.. But the bytearray can be anything like a pdf or jpg or png or txt file ..Shadoof
B
0

MediaType mediaType = result.getHeaders().getContentType();

Boccioni answered 4/11, 2016 at 16:9 Comment(2)
Hi Andy, Thanks for the Response. Yes this will give me MediaType but only the one set in acceptHeader.. But the bytearray can be anything like a pdf or jpg or png or txt file ..Shadoof
It's not the one set in the accept header, it's set by the server when it creates the response. If the server sets the MediaType based on what the byte array represents (e.g. application/pdf for a pdf file), then you can use the Content-Type header as such.Boccioni
M
0
RestTemplate restCall = new RestTemplate();
String uri = ""; //Your URL
byte[] content = restCall.getForObject(uri, byte[].class);

This code uses the RestTemplate class to make an HTTP GET request to a specified URL. The uri variable holds the URL, and getForObject(uri, byte[].class) fetches the content as a byte[] (binary data). The response is stored in the content variable as a byte array. This approach is commonly used to download binary files, like images or PDFs, from a web service. The RestTemplate simplifies handling HTTP requests and responses in Spring applications.

Messina answered 2/8 at 5:40 Comment(1)
Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.Joh

© 2022 - 2024 — McMap. All rights reserved.