Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding data to multipart request not from file #1028

Closed
davidsoncasey opened this issue Mar 3, 2016 · 1 comment · Fixed by #1493
Closed

Adding data to multipart request not from file #1028

davidsoncasey opened this issue Mar 3, 2016 · 1 comment · Fixed by #1493

Comments

@davidsoncasey
Copy link

I've been working using Rack to submit multipart requests to the Apple News API. This works fine when just submitting data from a JSON file, but there are cases in which an additional metadata part is required and is not expected to have the filename field in the Content-Disposition header. Here is the example from the Apple News Documentation:

--535e329ca936f79a19ac9a251f7d48f7
Content-Type: application/json
Content-Disposition: form-data; name=metadata

{
   "data": {
        "revision": "AAAAAAAAAAAAAAAAAAAAev=="
    }
}
--535e329ca936f79a19ac9a251f7d48f7

The issue that I was running into when using Multipart::build_multipart is that it if there is no original_filename attribute, then it uses Generator#content_for_other to create this part of the request, which does not specify Content-Type (lines 83-90 of multipart/generator.rb). If I were to use a file (or file like object) and build this with Multipart::UploadedFile, then the filename is included in the Content-Disposition, which I do not want.

What I was hoping to see (and I'd be willing to help contribute to) is a class similar to Multipart::UploadedFile that instead of receiving the path to a file upon initialization could simply receive a string of the content, and corresponding changes to Multipart::Generator that would allow for an instance of this class to be passed to the generator and build the part of the request correctly.

I'm not sure if this is an issue with other APIs that expect multipart requests, but for the Apple News API there are several different metadata fields that we will need to be submitting, so this functionality would be very useful. Thanks for all the hard work on Rack and let me know if I can provide any clarification or help make these changes.

@jeremyevans
Copy link
Contributor

It does seem useful to make it possible to generate that format. I have an implementation that I will submit in a pull request. As it requires a new (though backwards-compatible) API for UploadedFile, I'll want review by another core team member before merging it.

jeremyevans added a commit to jeremyevans/rack that referenced this issue Jan 17, 2020
This changes the UploadedFile API to use keyword arguments, being
backwards compatible with the previous API by keeping the positional
arguments and making the keyword defaults be the positional argument
values.

The two new arguments to UploadedFile#initialize are filename and io.
filename allows overriding the filename that will be used, instead of
always using the base name of the path.  io specifies an IO-like
object that can be read from to get the data, overridding any
provided path.  This allows you to generate UploadedFile instances
that previously were not possible without accessing the file system.

This changes the multipart generator to only include a Content-Length
for files with paths, and only include a filename in
Content-Disposition if the UploadedFile has one.

Fixes rack#1028.
ioquatix pushed a commit that referenced this issue Jan 17, 2020
This changes the UploadedFile API to use keyword arguments, being
backwards compatible with the previous API by keeping the positional
arguments and making the keyword defaults be the positional argument
values.

The two new arguments to UploadedFile#initialize are filename and io.
filename allows overriding the filename that will be used, instead of
always using the base name of the path.  io specifies an IO-like
object that can be read from to get the data, overridding any
provided path.  This allows you to generate UploadedFile instances
that previously were not possible without accessing the file system.

This changes the multipart generator to only include a Content-Length
for files with paths, and only include a filename in
Content-Disposition if the UploadedFile has one.

Fixes #1028.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants