question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending JSON data along with file

See original GitHub issue

I’m trying to send a file with some metadata to a Spring Controller at the backend, but I’m not having much luck. I think I narrowed the problem down to the absence of a content-type to go along with the json-data.

The request send is something along the lines of…

------WebKitFormBoundarywdhA8TIFsWhbSoX9 Content-Disposition: form-data; name=“meta-data”

{“metadata”:{…}} ------WebKitFormBoundarywdhA8TIFsWhbSoX9 Content-Disposition: form-data; name=“file”; filename=“2014-03-invoice.pdf” Content-Type: application/pdf

In order for my Spring controller to recognize the meta-data as JSON (to map it to an object defined in my backend) the content type needs to be set to application/json, as shown in these docs… http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart-forms-non-browsers

I’ve tried using the formDataAppender but I’m either not using it right, or it isn’t possible to accomplish this with it.

My Spring controller (for completeness’ sake)

    @RequestMapping(value = "/processwithfiles/", method = RequestMethod.POST)
    public void processWithFiles(@RequestPart("meta-data") final Document document,
            @RequestPart("file") final MultipartFile[] files, final HttpServletResponse response) {
        ...
    }

This throws an HttpMediaTypeNotSupportedException “Content type ‘application/octet-stream’ not supported”

I’ve noticed that when I use the annotation @RequestParam instead, it works, but only if I change the type of the meta-data to String. That would require me to parse this string to a JSON object myself.

Is there a way make sure the request looks like the one in the example… http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart-forms-non-browsers

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
danialfaridcommented, Dec 15, 2014

You cannot set the type of individual parts of multipart form data request, so it will be received as a string and need to be parsed back to json. The cleaner way is to upload the file in one service call and return an id to the client then send that id along with metadata in a separate normal post call and then on the server relate the file and metadata using that id.

1reaction
danialfaridcommented, Sep 23, 2015

only blob can have type

Read more comments on GitHub >

github_iconTop Results From Across the Web

Posting a File and Associated Data to a RESTful WebService ...
Send the file first in a multipart/form-data POST, and return an ID to the client. The client then sends the metadata with the...
Read more >
How to upload a file and post JSON data in the same request
var client = new JsonServiceClient(baseUrl); var fileToUpload = new FileInfo("path/to/file.txt"); var response = client.PostFile< ...
Read more >
How to send application/json data along with file in postman ...
Try giving your json object key as key parameter and value as value parameter in the form-data. 1
Read more >
How to send application JSON data along with a file in ... - Quora
For rest of "text" based parameters, you can post it like normally you do with postman. Just enter parameter name and select "text"...
Read more >
how to send "file" and json data with same request with ...
frmData.append(f1.name, f1.read());. // sending rawbytes along with file name. Did ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found