By Harikrishnan K.N.


2016-08-19 10:43:59 8 Comments

I am using Spring MVC and this is my method:

/** 
* Upload single file using Spring Controller 
*/ 
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST) 
public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler(@RequestParam("name") String name, @RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) { 
    if (!file.isEmpty()) { 
        try { 
            byte[] bytes = file.getBytes();     
            // Creating the directory to store file 
            String rootPath = System.getProperty("catalina.home"); 
            File dir = new File(rootPath + File.separator + "tmpFiles"); 
            if (!dir.exists()) 
                dir.mkdirs();     
            // Create the file on server 
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name); 
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile)); 
            stream.write(bytes);
            stream.close(); 
            System.out.println("Server File Location=" + serverFile.getAbsolutePath());
            return null; 
        } catch (Exception e) { 
            return null; 
        } 
    } 
} 

I need to pass the session id in postman and also the file. How can I do that?

10 comments

@Chris F Carroll 2018-05-25 14:41:58

I think the visual guide we're missing here is, highlighting the tasteful-but-nearly-invisible pale-grey-on-white dropdown for File.

After you choose POST, then choose Body->form-data, then find the dropdown, and then choose 'File', only then will the 'Choose Files' button magically appear:

Postman POST file setup - (Text,File) dropdown highlighted

@Sebastian Ardila 2018-10-10 20:45:43

If somebody wants to send json data in form-data format just need to declare the variables like this

Postman:

As you see, the description parameter will be in basic json format, result of that:

{ description: { spanish: 'hola', english: 'hello' } }

@Ajay k 2018-10-26 14:08:33

If you need like Upload file in multipart using form data and send json data(Dto object) in same POST Request

Get yor JSON object as String in Controller and make it Deserialize by adding this line

ContactDto contactDto  = new ObjectMapper().readValue(yourJSONString, ContactDto.class);

@kubilay 2018-06-04 08:00:08

If you want to make a PUT request, just do everything as a POST request but add _method => PUT to your form-data parameters.

@burakozgul 2016-08-19 10:51:24

Like this :

enter image description here

Body -> form-data -> select file

You must write "file" instead of "name"

Also you can send JSON data from Body -> raw field. (Just paste JSON string)

@Harikrishnan K.N. 2016-08-19 11:11:14

actually i need to pass some values and file in postman for one service is that possible

@Green 2017-09-06 12:05:26

Headers? what headers to set?

@Rohit 2018-05-03 07:08:38

The question asks for uploading json data as well, which is not covered here in this answer.

@Suman Das 2018-06-14 13:57:20

The correct way to do this : stackoverflow.com/questions/21329426/…

@Sebastiao Marcos 2018-01-02 00:38:45

If somebody needed:

body -> form-data

Add field name as array

enter image description here

@Rohit Thakur 2017-11-02 09:00:40

  1. Don't give any headers.
  2. Put your json data inside a .json file.
  3. Select your both files one is your .txt file and other is .json file for your request param keys.

@Kiran 2018-04-11 19:57:08

This is a good answer.. This can also be used for use cases where multiple file has to be uploaded along with a json payload in a multi-part payload..

@Anto 2018-07-17 10:59:25

This is the most correct answer if you want to send a File as well as JSON data. Examples showing selecting a file and then the JSON data being passed as a single JSON string value or splitting the values up do not work (possibly depending on the end point but I can't verify this). It is also mentioned here: github.com/postmanlabs/postman-app-support/issues/3331

@gce 2017-05-04 15:01:00

Maybe you could do it this way:

postman_file_upload_with_json

@Green 2017-09-06 12:02:50

What 6 headers have you set there?

@Gaurav Sharma 2018-01-18 13:40:04

This solution works for me. Didn't pass any headers and it's working fine. Thanks. Passing JSON was the tricky part for me. :)

@Chris Koston 2018-02-22 02:03:52

Ha, clever - changed my endpoint to do that :) A bit ugly but super convenient

@Sumit Badaya 2016-08-19 11:26:08

In postman, set method type to POST.

Then select Body -> form-data -> Enter your parameter name (file according to your code)

and on right side next to value column, there will be dropdown "text, file", select File. choose your image file and post it.

For rest of "text" based parameters, you can post it like normally you do with postman. Just enter parameter name and select "text" from that right side dropdown menu and enter any value for it, hit send button. Your controller method should get called.

@Green 2017-09-06 12:03:19

Why POST? What about PUT?

@Green 2017-09-06 12:04:54

Doesn't work. What headers should be set?

@Sumit Badaya 2017-09-07 04:16:42

What do you mean by "Doesnt work"? Why dont you show your code for us to debug issue easily?

@Arun Gowda 2018-09-03 11:10:04

I get the text part as String always. Why does it not map to my DTO? @PostMapping ( value = "/byImageFile", consumes = { "multipart/form-data" }) public ResponseEntity<?> postMap( @RequestPart ( "imageFile") MultipartFile imageFile, @RequestPart ( "fieldsToExtract") RequestDto requestDto ) Iam not able to send the requesst from postman at all in this case

@Sumit Badaya 2018-09-04 04:50:51

@ArunGowda Try posting your query as new Question. As it is different from the question poster here and answer given is relevant to question posted here by OP.

@Arun Gowda 2018-09-04 13:22:27

I found my solution. It won't map to RequestDtoautomatically. I took it as a string, parsed the Json and mapped it to RequestDto explicitly.

@rammanoj 2018-12-14 05:22:00

I want to send the file along with the json data not with the form-data, is there any way possible to do it ?

@Sandesh Jain 2016-08-19 10:58:14

If you are using cookies to keep session, you can use interceptor to share cookies from browser to postman.

Also to upload a file you can use form-data tab under body tab on postman, In which you can provide data in key-value format and for each key you can select the type of value text/file. when you select file type option appeared to upload the file.

@Harikrishnan K.N. 2016-08-19 13:23:32

i am getting 500 error

@Sandesh Jain 2016-08-23 12:40:52

For what you are getting 500. can you please share some more details

Related Questions

Sponsored Content

54 Answered Questions

[SOLVED] How can I pretty-print JSON in a shell script?

17 Answered Questions

25 Answered Questions

[SOLVED] How can I pretty-print JSON using JavaScript?

45 Answered Questions

[SOLVED] Can comments be used in JSON?

  • 2008-10-28 20:39:03
  • Michael Gundlach
  • 1608425 View
  • 6198 Score
  • 45 Answer
  • Tags:   json comments

9 Answered Questions

[SOLVED] Parsing values from a JSON file?

  • 2010-05-14 15:54:20
  • michele
  • 2333395 View
  • 1286 Score
  • 9 Answer
  • Tags:   python json parsing

6 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

39 Answered Questions

[SOLVED] How do I format a Microsoft JSON date?

16 Answered Questions

[SOLVED] Parse JSON in JavaScript?

33 Answered Questions

[SOLVED] What is the correct JSON content type?

  • 2009-01-25 15:25:19
  • Oli
  • 2344980 View
  • 9461 Score
  • 33 Answer
  • Tags:   json content-type

0 Answered Questions

upload imageURI to server MultipartForm

Sponsored Content