rpula123 | Posts: 23

RETRIEVE A ZIP FILE OF DOCUMENTS

0 votes
Hi, Do you have a sample code on how we can convert the byte stream response returned by GET /packages/{packageId}/documents/zip API into a zip file from the returned bytes. Thanks Rajesh

Duo_Liang | Posts: 3776

Reply to: RETRIEVE A ZIP FILE OF DOCUMENTS

0 votes
Hey Rajesh, Just a quick question, are you still using Spring RestTemplate calling REST API? After you confirmed, I will create some sample codes for you. Duo

Duo_Liang | Posts: 3776

Reply to: RETRIEVE A ZIP FILE OF DOCUMENTS

0 votes
After a quick test, below code works for me:
package com.oss.java.springdemo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class restTemplate_save_zip_file {
	public static void main(String[] args) throws IOException {
		String url = "https://sandbox.esignlive.com/api/packages/HHyaOMtEKih9Tw4Cmk2txwZv-4w=/documents/zip";

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.add("Authorization", "Basic your_api_key");
		HttpEntity> request = new HttpEntity>(null,
				headers);
		ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, request, byte[].class);

		if (response.getStatusCode() == HttpStatus.OK) {
			Files.write(Paths.get("HHyaOMtEKih9Tw4Cmk2txwZv-4w=.zip"), response.getBody());
		}
	}
}

rpula123 | Posts: 23

Reply to: RETRIEVE A ZIP FILE OF DOCUMENTS

0 votes
Thank You Duo for sample code. I am using rest template to invoke the API.

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off