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()) 
            // Create the file on server 
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name); 
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile)); 
            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?


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

You must first find the nearly-invisible pale-grey-on-white dropdown for File which is the magic key that unlocks the Choose Files button.

Visual Guide

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

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

@otamega 2019-05-05 06:23:01

Postman multipart form-data content-type

Select [Content Type] from [SHOW COLUMNS] then set content-type of "application/json" to the parameter of json text.

@vinci mojamdar 2019-03-27 10:12:37

I needed to pass both: a file and an integer. I did it this way:

  1. needed to pass a file to upload: did it as per Sumit's answer.

    Request type : POST

    Body -> form-data

    under the heading KEY, entered the name of the variable ('file' in my backend code).

    in the backend:

    file = request.files['file']

    Next to 'file', there's a drop-down box which allows you to choose between 'File' or 'Text'. Chose 'File' and under the heading VALUE, 'Select files' appeared. Clicked on this which opened a window to select the file.

2. needed to pass an integer:

went to:


entered variable name (e.g.: id) under KEY and its value (e.g.: 1) under VALUE

in the backend:

id = request.args.get('id')


@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


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);

@Abhisek Roy 2019-04-10 05:52:53

Can you tell me what are the two headers used?

@Ajay k 2019-05-14 07:40:09

Content-Type : application/json Authorization : bearer (yourTokenString)

@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.

@dassum 2018-06-14 13:57:20

The correct way to do this :…

@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:

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

Maybe you could do it this way:


@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 ?

@Patel Romil 2019-03-05 09:58:05

@SumitBadaya can i send file using raw option with other paramters in json form?

@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

13 Answered Questions

[SOLVED] How do I write JSON data to a file?

  • 2012-09-06 22:21:21
  • user1530318
  • 1238923 View
  • 919 Score
  • 13 Answer
  • Tags:   python json

47 Answered Questions

[SOLVED] Can comments be used in JSON?

  • 2008-10-28 20:39:03
  • Michael Gundlach
  • 1793163 View
  • 6627 Score
  • 47 Answer
  • Tags:   json comments

40 Answered Questions

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

20 Answered Questions

9 Answered Questions

[SOLVED] Why can't Python parse this JSON data?

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

34 Answered Questions

[SOLVED] What is the correct JSON content type?

54 Answered Questions

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

6 Answered Questions

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

23 Answered Questions

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

16 Answered Questions

[SOLVED] Parse JSON in JavaScript?

Sponsored Content