httr icon indicating copy to clipboard operation
httr copied to clipboard

Need a way to set Content-Type for individual parts of a multipart post

Open iargent opened this issue 11 years ago • 13 comments

At the moment, it's possible to set the Content-Type for a whole POST request using:

add_headers("Content-Type" = "multipart/related")

What appears to be missing is the ability to set the Content-Type for the individual parts of a multipart POST request. This is useful for scenarios like uploading a file together with its metadata to Google Drive, using the Drive API .

Would it be possible to provide a way to set headers for individual parts of a multipart post?

This issue was prompted by a question on Stack Overflow.

iargent avatar Jun 27 '15 05:06 iargent

Ping, I ran into this as well. If there's an preference for how to fix it I would be happy to do it myself, but I wouldn't want to intercede to fix without having an idea of the conventions.

thomascjohnson avatar Aug 22 '15 01:08 thomascjohnson

The curl vignette shows to set the content type of an individual file within the multipart with form_file

h <- new_handle()
handle_setform(h,
  foo = "blabla",
  bar = charToRaw("boeboe"),
  description = form_file(system.file("DESCRIPTION")),
  logo = form_file(file.path(Sys.getenv("R_DOC_DIR"), "html/logo.jpg"), "image/jpeg")
)
req <- curl_fetch_memory("http://httpbin.org/post", handle = h)

jeroen avatar Aug 22 '15 09:08 jeroen

Maybe I don't understand the problem. httr:: upload_file already lets you set the content type of the upload with the type argument. What exactly is missing?

jeroen avatar Aug 22 '15 09:08 jeroen

If you use a multipart type (e.g. "multipart/related") with httr, you need a way to set the type of each individual part. At the moment, there doesn't seem to be any way to do this.

My use case is uploading files and metadata to Google Drive. Quoting from the Google Drive API guide for uploading files:

Each part of the multipart request needs an additional Content-Type header:

  1. Metadata part: Must come first, and Content-Type must match one of the accepted metadata formats.
  2. Media part: Must come second, and Content-Type must match one the method's accepted media > MIME types.

It may be possible to do this with curl, but as far as I can see, you can't do it with httr.

iargent avatar Aug 22 '15 12:08 iargent

I still don't understand why the type argument in httr::upload_file (aka curl::form_file) does not suffice?

jeroen avatar Aug 22 '15 12:08 jeroen

I am sorry I'm not explaining it very well. It might help to look at this Stack Overflow question.

To upload a file and metadata (or two separate files) I need to post a multipart form with a content type for the whole thing, but also additional content types for each part - say, file 1 is image/jpeg and file 2 is text/json, for example. I effectively need to set three content types, but I can only set one.

Is that explanation any better?

iargent avatar Aug 22 '15 12:08 iargent

You can set a content type for each individual upload_file in httr. Let me create an example.

jeroen avatar Aug 22 '15 12:08 jeroen

Here is some code that follows the example from the API doc. Can you test if this does the job: https://gist.github.com/jeroenooms/a68cd7908e1dbea89535

The only difference is that curl posts some additional Content-Disposition headers but google will probably just ignore those.

jeroen avatar Aug 22 '15 12:08 jeroen

That looks like exactly what I need - thanks very much! Your code example is very clear and I think I can see what I was doing wrong. I can't test right now, but I will do so in the next few hours and update this ticket.

iargent avatar Aug 22 '15 12:08 iargent

Okay, please make sure you are using the latest version of httr and curl because in the SO topic you are using a very old version.

jeroen avatar Aug 22 '15 12:08 jeroen

Thanks for the tip. I tried upgrading curl using devtools and got this, by the way:

install_github("jeroenooms/curl") Downloading github repo jeroenooms/curl@master Error in packageVersion("curl") : package ‘curl’ not found

I'll try installing from the shell. Is this something you want me to report?

iargent avatar Aug 22 '15 12:08 iargent

Just install the latest versions from CRAN. You can't install curl with devtools because devtools itself uses curl to download form github.

jeroen avatar Aug 22 '15 12:08 jeroen

The solution you gave works even with httr 0.6.1, though I am updating it to avoid any future problems.

Closing this ticket. Thanks again!

iargent avatar Aug 24 '15 09:08 iargent