3

I am using Spring framework in Java. With it I am trying to write an API endpoint that accepts one file for upload. I do NOT want to use form data. Basically I want the following curl command to work on my endpoint

curl -XPOST localhost:8080/upload -d @local_file.data

Is there a way to do that with Spring? It seems like I want to use @RequestBody with File but that somehow seems like a horrible idea to most people. Why? How is MultipartFile different from File? Only because it supports form data?

All I can find is talk of forms and MultipartFile. Is what I'm doing somehow inherently wrong? I've done it before using Python/Django and it wasn't that hard.

2
  • 2
    It way not be wrong, but without form-data you'd be in trouble creating a web-frontend for your app. If you know you will always and only curl into your app you should be fine. Commented Dec 8, 2015 at 5:43
  • Thanks. It's not really an app. It's a backend API service. Commented Dec 8, 2015 at 13:49

1 Answer 1

6

It seems like the answer to my question is to use @RequestBody with a byte array. This kicks in the ByteArrayHttpMessageConverter which is one of the default HttpMessageConverters. I end up with an API endpoint like this.

@RequestMapping(value = "/upload", method=RequestMethod.POST)
public Response uploadFile(@RequestBody byte[] file) {
  ...
}

Preliminary testing works. Found it in Spring docs here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestbody

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.