Spring Calling External API with two files - multipart/form-data - java

I have an external API that I am invoking. The API takes two files as input. One File is .txt and other file is .JOSN.
I need to call this API through spring. This api is multipart/form-data.
public String doAPICall(String uri, String token) {
File idFile = new File ("c:\tmp\id_file.txt");
File jsonFile = new File ("c:\tmp\jsonFile.json");
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("idFile", new FileSystemResource(idFile));
body.add("jsonFile", new FileSystemResource(jsonFile));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("Authorization", "Bearer " + token);
HttpEntity<MultiValueMap<String, Object>> requestEntity= new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.exchange(
uri, HttpMethod.POST,requestEntity,
String.class).getBody().toString();
System.out.println(result);
return result;
}
Result: 500 - Internal Server Error. However, If I invoke the API with the same config via Insomnia/Postman, it works.
Also, it's not throwing 401 as the token is valid.
Do I need to set some extra header as I am uploading a txt and JSON file?
PS: I have typed the code here, the code might not be well-formatted.
Please share if anyone has any suggestions. Thank you!

Related

How to upload hosted file over the Rest Template?

I want to pass a hosted file over the rest template to another end point. I have to pass the hosted file URL as a String.
See the below code.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add(ATTACHMENT,new FileSystemResource(request.getAttachment()));
HttpEntity<MultiValueMap<String,Object>> outBoundRequest = new HttpEntity<>(map, headers);
return restTemplate.exchange(uri[0],HttpMethod.POST,outBoundRequest,Response.class);
Suppose I am passing "http://www.africau.edu/images/default/sample.pdf";
Why I am getting "Illegal char <:> at index 4: http:\www.africau.edu\images\default\sample.pdf"?
Can anyone help me out here.
Thanks,
Dasun.

Spring Rest Template Injects Value into Parameter with %

Can someone please explain to me what is happening in the following code?
If I run:
RestTemplate rest = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("header1", "data");
headers.add("header2", "data");
HttpEntity<String> entity = new HttpEntity<>("body", headers);
ResponseEntity<JSONObject> response =
rest.exchange("https://example.com/../{username}/{path}", HttpMethod.GET, entity, JSONObject.class, "user%123", "path");
Spring tells me that the URL used is https://example.com/../username/path where username = user%25123 even though I set the username = user%123.
This is looking to me to be not a Spring issue, rather a URL encoding issue, but can anyone tell me where this extra 25 comes from?
%25 is a URL-encoded percent sign, so user%123 encodes to user%25123
More info: https://www.w3schools.com/tags/ref_urlencode.ASP

Creating a Bitbucket-Repository in Java via REST

I am trying to create a Bitbucket repository using their REST-API. Everything seems to work except setting the "parent" project, where the repository needs to be created in. On this link a cURL example is provided. In the body, the parameter "scm" is set as either "git" or "hg", both being Strings, the parameter "project" seems to be a json object containing a key-value pair. Everything I tried so far did not work (json object, string, etc.)
Question: How can I create a repository IN a specific project?
My code looks the following:
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.bitbucket.org/2.0/repositories/" + tName + "/" + rName;
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic 1234567890qwert");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
MultiValueMap<String, String> project = new LinkedMultiValueMap();
project.add("key", "aaaaaaaa"); //the repo should be created in the project aaaaaaaa
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
postParameters.add("scm", "hg"); //hg or git, does not matter
postParameters.add("project", project); //<-- the api ignores the declared project
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(body, headers);
ResponseEntity<BitbucketRepository> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, BitbucketRepository.class);
System.out.println("createRepository: " + response);
return response;
You could use the bitbucket-rest library to do this like so:
BitbucketClient client = BitbucketClient.builder()
.endPoint("http://127.0.0.1:7990")
.credentials("admin:password") // will base64 for you if not already done. Can optionally use token auth as well.
.build();
CreateRepository crepo = CreateRepository.create("my-repo", true);
Repository repo = client.api().repositoryApi().create("my-project", crepo);

Walmart (Java integration)- post feed with Resttemplate

I'm currently integrating Walmart into our application and I'm trying to post an Item Feed through the API method.
I've already succeeded in posting a feed from the ARC tool, so I already know how to model the post request for it, my only problem is that I'm having difficulties in using it in my code, while using the Resttemplate.
Below is the request (from ARC - Chrome) that succeeded to post my item (I can see them in my Walmart Seller console) and the Java code for it (trimmed the credentials for security reason):
Obs: The signature and the other fields used in the Java code for the requests are correctly generated, because I used them in my request from ARC as well, so there's no issue in the headers' values, but in how I'm trying to set the file in the body request, as far as I can tell:
And the code for generating the headers and sending the post request with Resttemplate:
..............................
headers = new HttpHeaders();
headers.set("WM_SVC.NAME", "Walmart Marketplace");
headers.set("WM_QOS.CORRELATION_ID", "123456abcdef");
headers.set("WM_SEC.TIMESTAMP", timestamp);
headers.set("WM_SEC.AUTH_SIGNATURE", signatureString);
headers.set("WM_CONSUMER.ID", "my-consumer-id-cut-for-security-reasons");
headers.set("WM_CONSUMER.CHANNEL.TYPE", "my-channel-type");
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
headers.set("Host", "https://marketplace.walmartapis.com");
..............................
File file = new File("C:\\walmartfeed.xml");
DiskFileItem fileItem = new DiskFileItem("file", "text/xml", false, file.getName(), (int) file.length(), file.getParentFile());
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
MultiValueMap<String, Object> parts =
new LinkedMultiValueMap<String, Object>();
parts.add("file", new ByteArrayResource(multipartFile.getBytes()));
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(parts, headers);
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
restTemplate.getMessageConverters().add(new MultipartAwareFormHttpMessageConverter());
ResponseEntity<FeedAcknowledgement> response = restTemplate.exchange("https://marketplace.walmartapis.com/v3/feeds?feedType=item", org.springframework.http.HttpMethod.POST, request, FeedAcknowledgement.class);
I am receiving a 400 status code saying it's a bad request.
Did you manage to post feeds for Walmart or is there any other way to use Resttemplate to post multipart/form-data / files ?
Thanks

Filename wrong encoding when has accent character at upload file with RestTemplate

I'm trying to upload a file and basically i have a issue with filename encoding, when a send a multipart/form-data with RestTemplate and filename contains character accent like "tésté.pdf", server responds "t?st?.pdf", but when i send with postman/browser it works!
I tried a lot of things, like set MessageConverters with utf-8, convert string to byte and create a new one with utf-8 and set at File, create a new file with name already converted in utf-8 and etc.. I have no more ideas to resolve this problem, i'm using latest version of spring-webmvc, 4.3.2, and still does'nt work.
String url = "https://someurl.com/api";
RestTemplate t = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
File file = new File("/Users/myuser/Desktop/tésté.pdf");
map.add("file", new FileSystemResource(file));
map.add("convert", "false");
map.add("thumb", 0);
String postForObject = t.postForObject(url, new HttpEntity<LinkedMultiValueMap>(map, headers), String.class);

Categories

Resources