rnmp
rnmp

Reputation: 87

Post request with file data in headers. Good practice?

Context

I've written a JS function that sends a POST request to my API endpoint with the contents of an image file. Part of the requirement is to also send file-related information as part of the request. Encoding the request as multipart is not an option for me, so the solution I've written involves putting the information as part of the request headers.

Question

I noticed on a related question that this approach is not suggested as an option, so I'm wondering if my solution is not considered a good practice and if there's any potential downsides. So far the code I wrote works as expected, but I wanted to gut check here.

Upvotes: 0

Views: 870

Answers (1)

Evert
Evert

Reputation: 99571

Adding information about the HTTP request to the header is not entirely without precedence. For instance, you could consider that Content-Type, Title, Link isn't just 'meta-data', it's relevant data.

I don't think there's anything in the specifications that explicitly forbid this. I think the biggest issue with it is that it's 'surprising' behavior, and it's good to try to build API's that are the least unique and/or surprising.

Some random alternatives, not necessarily in order of preference.

  • Use separate HTTP requests
  • Use multipart formats (this is super common and doesn't have to lead to exactly 33% increases in size. Compression exists)
  • Maybe a bit outlandish, but you can embed meta-data in EXIF or add new components to .png files.
  • It wouldn't be that difficult to create a new format that starts with meta-data and is followed with the full binary image file

I'll leave it up to you to decide what's best, but I don't think there's anything non-standard about what you're doing.

Upvotes: 1

Related Questions