I’m working with external API , when I execute one of the request that returns pdf I cannot handle it in the server side .
If I copy paste the api request : https://api.worldota.net/api/b2b/v3/hotel/order/document/voucher/download/?data={“partner_order_id” : “0d7836b4-2eba-475f-b2d6-1b95092534b0” , “language”:”en”}
the browser open a pdf in the browser and download it . when I execute this url in the java code how do I return the response as pdf download ? to the client ? which object should I return ?
my code :
try { OkHttpClient client = new OkHttpClient().newBuilder() .addInterceptor(new BasicAuthInterceptor("api-key", "api-key")).build(); Request request = new Request.Builder().url( "https://api.worldota.net/api/b2b/v3/hotel/order/document/voucher/download/?data={"partner_order_id":"" + partner_order_id + "","language":"" + language + ""}") .method("GET", null).addHeader("Content-Type", "application/pdf").build(); Response response = client.newCall(request).execute(); response.close(); } catch (Exception e) { } return ResponseEntity.ok("downloaded completed");
thanks for helping . idan,
Advertisement
Answer
Path path = Paths.get(“https://api.worldota.net/api/b2b/v3/hotel/order/document/voucher/download/?data={“partner_order_id”:”” + partner_order_id + “”,”language”:”” + language + “”}”); byte[] data = Files.readAllBytes(path); ByteArrayResource resource = new ByteArrayResource(data);
return ResponseEntity.ok() // Content-Disposition .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + path.getFileName().toString()) // Content-Type .contentType(mediaType) // // Content-Lengh .contentLength(data.length) // .body(resource);