43

I'm using Postman to send the following request: enter image description here

My controller looks like this:

@RestController
@RequestMapping(path = RestPath.CHALLENGE)
public class ChallengeController {

    private final ChallengeService<Challenge> service;

    @Autowired
    public ChallengeController(ChallengeService service) {
        this.service = service;
    }

    @ApiOperation(value = "Creates a new challenge in the system")
    @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE},
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    public ChallengeDto create(@ApiParam(value = "The details of the challenge to create") @RequestPart("challengeCreate") @Valid @NotNull @NotBlank ChallengeCreateDto challengeCreate,
                           @ApiParam(value = "The challenge file") @RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
        return service.create(challengeCreate, file);
    }
}

I already tried to change the "consumes" to delete the APPLICATION_OCTET_STREAM_VALUE to MULTIPART_FORM_DATA_VALUE and also tried to delete it, but none of these helped.

Please tell me if you need more information. Thanks.

StackJP
  • 541
  • 1
  • 5
  • 14

6 Answers6

80

@Rokin Answer is good but dont need to place json in file and upload. You can achieve by passing content type to json object as well. Postman support content type options while sending form-data. for further see the below image which is self descriptive.

enter image description here

Irfan Nasim
  • 1,952
  • 2
  • 19
  • 29
  • 4
    Since this is the most upvoted answer, I am adding this extra gist obtained from @John's answer - Content-Type is a header setting which defaults to `application-octet-stream`. To avoid the error, set the `Content-Type` value in header to contain `application/json`. – paradocslover May 28 '21 at 07:24
22

For Spring's @RequestPart to work with json objects, in Postman - you need to send the json object as a File instead of Text.

Put the contents of ChallengeCreateDto in a json file and save it as challenge.json. Then upload this file in Postman with the type as File. I've attached a screenshot of how the request in Postman should be just to make it more clearer.

enter image description here

You can also use @PostMapping instead of @RequestMapping in the newer versions of Spring as shown here

@ResponseStatus(HttpStatus.CREATED)
@PostMapping()
public ChallengeDto create(@RequestPart("challengeCreate") ChallengeCreateDto challengeCreate, 
    @RequestPart("file") MultipartFile file) {
        return service.create(challengeCreate, file);
    }

Rokin Maharjan
  • 629
  • 7
  • 19
  • 14
    Alternativelly you can show the column "Content Type" via three dots menu, and set conent type to `application/json`. Easier than saving json data as a file first. – Denis K Jan 22 '20 at 11:41
  • 2
    @DenisK really nice hint. – Cyber Mar 07 '20 at 11:07
  • This is a hack, for anyone even considering using this - don't. Check out Irfan's answer above. – Anomitra Aug 22 '22 at 07:48
6

Using @RequestParam to get string and file will solve this issue. In Postman use Content-Type as "multipart/form-data" and in Body define your input as form-data.

Refer https://stackoverflow.com/a/38336206/1606838

Example:

 @PostMapping(consumes = {"multipart/form-data"})
public Output send(@RequestParam String input, @RequestParam MultipartFile file) {

}
Piyush_Chandra
  • 111
  • 1
  • 9
3

Content-Type is a header setting and by default it is not checked and defaults to application-octet-stream. Just check the box under the headers ribbon item (which once checked defaults to application/json). enter image description here

John
  • 214
  • 3
  • 14
0

this solved my issue by passing content type

Garbit
  • 5,805
  • 6
  • 39
  • 72
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 09:36
0

I faced the same issue, but it's solved. Check my image link

enter image description here

Resolved

[org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/octet-stream' is not supported]
Hibernate: select next_val as id_val from product_seq for update

So use the content-type in the body. It worked for me.

DaveL17
  • 1,673
  • 7
  • 24
  • 38