• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

How to send multipart/form-data with Spring RestTemplate?

April 10, 2020 by

Questions › How to send multipart/form-data with Spring RestTemplate?
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I want to send a POST request to a rest endpoint. the rest endpoint documentation says:

Create a node and add it as a primary child of node nodeId.

This endpoint supports both JSON and multipart/form-data (file upload).

Using multipart/form-data

Use the filedata field to represent the content to upload, for example, the following curl command will create a node with the contents of test.txt in the test user's home folder.

curl -utest:test -X POST host:port/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children -F filedata=@test.txt

You can use the name field to give an alternative name for the new file.

You can use the nodeType field to create a specific type. The default is cm:content

I managed to send a correct request to this endpoint by the following code:

@Override
public ResponseEntity<byte[]> postNode(String nodeId, byte[] content) {
  MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
ByteArrayResource contentsAsResource = new ByteArrayResource(content) {
    @Override
    public String getFilename() {
        return "name22222";
    }
};

  bodyMap.add("filedata", contentsAsResource);

  ///bodyMap.add("filedata", content);// why this does not work??!

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
        return restTemplate.exchange("/nodes/{nodeId}/children", HttpMethod.POST, requestEntity, byte[].class, nodeId);
}

but I have two questions:

1 – why the commented line does not work?

2 – the docs says :

You can use the name field to give an alternative name for the new file.

I did not use a "name" field, but the server saved my files with the correct name(="name22222"), why?(I thought multipart/form-data is a kind of a simple name-value, if this is correct then I have a field named "filedata" and its value is my byte array content, so how the file name is send?). and how can I specify the file name with a field?

update : i think i found my answers. i just need to read about multipart/form-data!

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: http, java, spring, web

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Copyright © 2023