Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman: Required request part 'file' is not present

I wanted to upload an image to my Rest API through postman. I am using spring boot framework. Here is the screen shot:

enter image description here

I also have not set any any headers as I found in other stack overflow answers that it gives multipart boundary error.

Now, below is my controller code:

package com.practice.rest.assignment1.controller;

import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.practice.rest.assignment1.model.Product;
import com.practice.rest.assignment1.service.CatalogueService;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

@RestController
@RequestMapping("/CatalogueController/")
public class CatalogueController{

    @Autowired
    private CatalogueService catalogueService;

    @RequestMapping(value = "addProduct", method = RequestMethod.POST , consumes = "multipart/form-data")
    public Product addProduct(@RequestParam String productJson, @RequestParam MultipartFile file) throws JsonParseException, JsonMappingException, IOException {


        Product product = new ObjectMapper().readValue(productJson, Product.class);
        byte[] mediaBytes = file.getBytes();
        product.setImage(mediaBytes);
        return catalogueService.saveInDb(product);

    }

}

Now, I am taking a Product object which consists internally an image defined as byte[] array. I take this as string and image separately as Multipart file.

Below is my product class attributes defined :

    private Long pId;
    private String model;
    private String brand;
    private byte[] image; // This is where I want the image to save
    private Long price;
    private String currency;
    private String transmissionType;
    private String fuelType;

Since , I am using spring boot , here is my Main class :

package com.practice.rest.assignment1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App {

  public static void main(String[] args) {
      SpringApplication.run(App.class, args);  
  }

}

The error on postman I get is :

{
  "timestamp": 1478611635977,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.multipart.support.MissingServletRequestPartException",
  "message": "Required request part 'file' is not present",
  "path": "/CatalogueController/addProduct"
}

Where am I wrong ?

like image 968
Number945 Avatar asked Nov 08 '16 13:11

Number945


3 Answers

I think the problem lies in the JSON parameter you are sending. In postman you don't need to put the starting and trailing " to represent a parameter as string. And also if you use starting and ending " then inside the JSON( mean for JSON object the properties key and value) you should use '(single quote).

like image 113
Lipu Avatar answered Oct 13 '22 14:10

Lipu


Try remove 'Content-Type: multipart/form-data...', section of the headers. It solved this for me.

like image 22
kenadet Avatar answered Oct 13 '22 14:10

kenadet


For me it worked to set these variables in application.properties:

spring.http.multipart.enabled=true 
spring.http.multipart.location= /upload
like image 28
Radim Burget Avatar answered Oct 13 '22 16:10

Radim Burget