【问题标题】:How to POST multipart/related content with httr (for Google Drive API)如何使用 httr 发布多部分/相关内容(用于 Google Drive API)
【发布时间】:2015-09-13 19:57:39
【问题描述】:

我使用 httr 将简单的文件上传到 Google Drive。问题是每个文档都作为“无标题”上传,我必须修补元数据来设置标题。 PATCH 请求偶尔会失败。

根据API,我应该能够进行分段上传,允许我将标题指定为上传文件的同一个 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<-POST(
  "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&convert=true",
  config(token=google_token),
  body=list(y=upload_file(file),
            #add_headers("Content-Disposition" = "text/json"),
            json=toJSON(data.frame(title))
  ),
  encode="multipart",
  add_headers("Content-Type" = "multipart/related"),
  verbose()
)

我得到的输出显示各个部分的内容编码错误,导致400错误:

-> 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
<- 

有没有办法为各个部分正确设置内容编码?例如,第二部分应该是“text/json”。

我浏览过 R 文档、Github 上的 Hadley 的 httr 项目页面、这个网站和一些一般的谷歌搜索。我找不到任何关于如何进行分段上传和设置内容编码的示例。

【问题讨论】:

  • 嗯,我认为目前还没有(除了将 json 保存到磁盘并使用 upload_file())。你能在github上提交一个错误吗?
  • 很高兴!感谢您及时的回复。 Bug filed.
  • 您使用的是非常旧的httr 版本。请先更新。

标签: r file-upload google-drive-api multipart httr


【解决方案1】:

您应该能够使用curl::form_file 或其别名httr::upload_file 来执行此操作。另请参阅 curl vignette。按照 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)

这里唯一的区别是 curl 会自动为每个文件添加一个Content-Disposition 标头,multipart/form-data 需要,但multipart/related 不需要。在这种情况下,服务器可能会忽略这个多余的标头。

目前没有办法在不将内容写入文件的情况下完成此操作。也许我们可以在 httr/curl 的未来版本中添加类似的东西,尽管这在以前没有出现过。

【讨论】:

  • 将元数据写入临时文件对我来说已经足够了。此策略适用于 httr 版本 0.6.1 和 1.0.0.9000。
  • 注意上面的代码需要包“jsonlite”以及httr。
  • curl::form_file 旁边还有一个curl::form_data。并且这些实现看起来很简单,可以在必要时进行复制和修改。 github.com/jeroen/curl/blob/master/R/form.R
  • @jeroen 如何在单个 API 调用中上传多张图片?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多