Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3686

Making a MultiPart Put request via Spring Rest to call a API with formData (Replace Apache MultipartEntityBuilder with REST)

$
0
0

I have a PUT API to call to send my data that expects a Multipart Request. (Swagger has the API listed as Paramter Type formData; Data Type file).

I have this code working fine via Apache's Http Library, but to match the rest of the program I would like to use Spring Rest Template to make the same call.

//Via Apache:Uri uri = "http://putmyresults.com";ResultObject results = buildResultObject(myData);MultipartEntityBuilder meb = MultipartEntityBuilder.create();meb.addBinaryBody("file", results.convertToBytes(), ContentType.create("application/octet-stream"),"MyResultFile");HttpEntity entity = meb.build();put.setEntity(entity);put.setHeader(new BasicHeader("Authorization","myToken"));put.setHeader("Accept","application/json,application,octet-stream");getMyCloseableHttpClient().execute(put);//process response codes...//Sample working REST for GET request:Uri uri = "http://getmyresults.com/section5/resultId?sectionId=foo";RestTemplate rest = new RestTemplate;org.springframework.http.HttpEntity header = new HttpHeaders();header.set("Authorization","myToken");header.setContentType(MediaType.APPLICATION_JSON);header.setAccept(Arrays.asList(MediaType.APPLICATION_JSON,MediaType.APPLICATION_OCTET_STREAM));HttpEntity<String> entity = new HttpEntity("parameters", headers);rest.exchange(uri, HttpMethod.GET, entity, Resource.class);//process response...

But I can't seem to make the PUT request via REST. This is what I have so far:

Uri uri = "http://putmyresults.com";MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();ResultObject results = buildResultObject(myData);byte[] bytes = results.convertToBytes();HttpHeaders header = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);org.springframework.http.HttpEntity<byte[]> entity = new HttpEntity<>(bytes,headers);map.add("MyResultFile",entity);HttpHeaders tokenHeader = new HttpHeaders();tokenHeader.set(new BasicHeader("Authorization","myToken"));tokenHeader.setContentType(MediaType.APPLICATION_JSON);HttpEntity<String> tokenEntity = new HttpEntity("parameters", headers);map.add("parameters",tokenEntity);org.springframework.http.HttpEntity<<MultiValueMap<String,Object> requestMap = new org.springframework.http.HttpEntity<>(map);new RestTemplate().exchange(uri,HttpMethod.PUT,requestMap,String.class);

Server Header:

@PutMapping(path = "/xyz")public ResponseEntity putRequest{    HttpServletRequest request,    HttpServletResponse response,    @PathVariable String id,    @RequestParam("file") MultipartFile uploadedFile,    @RequestParam String sample,    @RequestParam String sample2 throws Exception  {        uploadedFile.getBytes(); ...    }}

Thanks for your help.


Viewing all articles
Browse latest Browse all 3686

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>