如何使用httr发布多部分/相关内容(适用于Google Drive API)

如何使用httr发布多部分/相关内容(适用于Google Drive API),r,file-upload,google-drive-api,multipart,httr,R,File Upload,Google Drive Api,Multipart,Httr,我使用httr将简单的文件上传到googledrive。问题是每个文档都是以“无标题”的形式上传的,我必须修补元数据以设置标题。补丁请求偶尔会失败 根据,我应该能够进行多部分上传,允许我在上传文件的同一POST请求中指定标题 res<-POST( "https://www.googleapis.com/upload/drive/v2/files?convert=true", config(token=google_token), body=list(y=upload_file(

我使用httr将简单的文件上传到googledrive。问题是每个文档都是以“无标题”的形式上传的,我必须修补元数据以设置标题。补丁请求偶尔会失败

根据,我应该能够进行多部分上传,允许我在上传文件的同一POST请求中指定标题

res<-POST(
  "https://www.googleapis.com/upload/drive/v2/files?convert=true",
  config(token=google_token),
  body=list(y=upload_file(file))
)
id<-fromJSON(rawToChar(res$content))$id
if(is.null(id)) stop("Upload failed")
url<-paste(
  "https://www.googleapis.com/drive/v2/files/",
  id,
  sep=""
)
title<-strsplit(basename(file), "\\.")[[1]][1]
Sys.sleep(2)
res<-PATCH(url,
  config(token=google_token),
  body=paste('{"title": "',title,'"}', sep = ""),
  add_headers("Content-Type" = "application/json; charset=UTF-8")
)
stopifnot(res$status_code==200)
cat(id)

res您应该能够使用
curl::form_file
或其别名
httr::upload_file
来执行此操作。另请参见卷曲。下面是Google API文档中的示例:

library(httr)

media <- tempfile()
png(media, with = 800, height = 600)
plot(cars)
dev.off()

metadata <- tempfile()
writeLines(jsonlite::toJSON(list(title = unbox("My file"))), metadata)

#post
req <- POST("https://httpbin.org/post",
  body = list(
    metadata = upload_file(metadata, type = "application/json; charset=UTF-8"),
    media = upload_file(media, type = "image/png")
  ),
  add_headers("Content-Type" = "multipart/related"),
  verbose()
)

unlink(media)
unlink(metadata)
库(httr)

媒体:嗯,我认为目前还没有其他方法(除了将json保存到磁盘并使用
upload\u file()
)。您能在github上提交一个bug吗?非常乐意!谢谢你的快速回复。您使用的是非常旧版本的
httr
。请先更新。将元数据写入临时文件对我来说已经足够了。此策略适用于httr版本0.6.1和1.0.0.9000。请注意,上述代码需要包“jsonlite”以及httr。在
curl::form_文件
旁边还有一个
curl::form_数据
。这些实现看起来非常简单,可以在必要时进行复制和修改@jeroen如何在一个API调用中上载多个图像?
-> POST /upload/drive/v2/files?uploadType=multipart&convert=true HTTP/1.1
-> User-Agent: curl/7.19.7 Rcurl/1.96.0 httr/0.6.1
-> Host: www.googleapis.com
-> Accept-Encoding: gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Authorization: Bearer ya29.ngGLGA9iiOrEFt0ycMkPw7CZq23e6Dgx3Syjt3SXwJaQuH4B6dkDdFXyIC6roij2se7Fs-Ue_A9lfw
-> Content-Length: 371
-> Expect: 100-continue
-> Content-Type: multipart/related; boundary=----------------------------938934c053c6
-> 
<- HTTP/1.1 100 Continue
>> ------------------------------938934c053c6
>> Content-Disposition: form-data; name="y"; filename="db_biggest_tables.csv"
>> Content-Type: application/octet-stream
>> 

>> table    rows    DATA    idx total_size  idxfrac

>> 
>> ------------------------------938934c053c6
>> Content-Disposition: form-data; name="json"
>> 
>> {"title":"db_biggest_tables"}
>> ------------------------------938934c053c6--

<- HTTP/1.1 400 Bad Request
<- Vary: Origin
<- Vary: X-Origin
<- Content-Type: application/json; charset=UTF-8
<- Content-Length: 259
<- Date: Fri, 26 Jun 2015 18:50:38 GMT
<- Server: UploadServer
<- Alternate-Protocol: 443:quic,p=1
<- 
library(httr)

media <- tempfile()
png(media, with = 800, height = 600)
plot(cars)
dev.off()

metadata <- tempfile()
writeLines(jsonlite::toJSON(list(title = unbox("My file"))), metadata)

#post
req <- POST("https://httpbin.org/post",
  body = list(
    metadata = upload_file(metadata, type = "application/json; charset=UTF-8"),
    media = upload_file(media, type = "image/png")
  ),
  add_headers("Content-Type" = "multipart/related"),
  verbose()
)

unlink(media)
unlink(metadata)