如何POST多部分二进制&带有httr的非二进制文件(适用于Salesforce API)

时间:2017-02-21 00:56:29

标签: r curl salesforce multipart httr

我在使用 httr 将文件发布到Salesforce's REST API时遇到了一些麻烦。我以为this SO question可能会照顾它,但似乎没有。我有一种预感,这是因为Saleforce想要一个非二进制部分而upload_file创建一个类form_file的对象,它被作为二进制文件处理,但也许有人有另一种解释/解决方案... < / p>

Salesforce要求以下curl请求插入新文档:

curl https://yourInstance.salesforce.com/services/data/v23.0/sobjects/Document/ -H
"Authorization: Bearer token" -H "Content-Type: multipart/form-data;
boundary=\"boundary_string\"" --data-binary @newdocument.json

newdocument.json 看起来像这样:

--boundary_string
Content-Disposition: form-data; name="entity_document";
Content-Type: application/json
{
"Description" : "Marketing brochure for Q1 2011",
"Keywords" : "marketing,sales,update",
"FolderId" : "005D0000001GiU7",
"Name" : "Marketing Brochure Q1",
"Type" : "pdf"
}
--boundary_string
Content-Type: application/pdf
Content-Disposition: form-data; name="Body"; filename="2011Q1MktgBrochure.pdf"
Binary data goes here.
--boundary_string--

如果我尝试使用@Jeroen的答案生成类似的输出作为有用的指南,我收到错误请求错误:

library(httr)
library(jsonlite)

url <- "https://[Salesforce Instance]/services/data/v39.0/sobjects/Document/"
header <- add_headers(c(Authorization = "Bearer [Salesforce Key]"))

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

metadata <- tempfile()
x <- data.frame(FolderId="a0a1300000ZG7u3", Name="test.png") #Salesforce Record ID and file name
json <- toJSON(unbox(x), pretty=T)
writeLines(json, metadata)

body <- list(entity_document = upload_file(metadata, type = "application/json; charset=UTF-8"), 
             Body = upload_file(media, type = "image/png"))


req <- POST(url, header, verbose(), body = body)

(详细输出)

-> POST /services/data/v39.0/sobjects/Document/ HTTP/1.1
-> Host: [Salesforce Instance]
-> User-Agent: libcurl/7.51.0 r-curl/2.3 httr/1.2.1.9000
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Authorization: Bearer [Salesforce Key]
-> Content-Length: 3720
-> Expect: 100-continue
-> Content-Type: multipart/form-data; boundary=------------------------6525413b2350e313
-> 
<- HTTP/1.1 100 Continue
>> --------------------------6525413b2350e313
>> Content-Disposition: form-data; name="entity_document"; filename="file1510059b5200f"
>> Content-Type: application/json
>> 

>> {
>>     "FolderId": "a0a1300000ZG7u3",
>>     "Name": "test.png"
>>   }

>> 
>> --------------------------6525413b2350e313
>> Content-Disposition: form-data; name="Body"; filename="file151001ac96950"
>> Content-Type: image/png
>> 

>> ‰PNG
>> 

>> 
>> --------------------------6525413b2350e313--

<- HTTP/1.1 400 Bad Request

除第一部分中的文件名外,这非常接近所需的输出,但Salesforce返回消息:

content(req)

[[1]]
[[1]]$message
[1] "Cannot include more than one binary part"

[[1]]$errorCode
[1] "INVALID_MULTIPART_REQUEST"

我只尝试了json,但获得了一个稍微不同的错误请求:

body <- list(entity_document= json, 
             Body = upload_file(media, type = "image/png"))

-> Content-Type: multipart/form-data; boundary=------------------------ecbd3787f083e4b1
-> 
<- HTTP/1.1 100 Continue
>> --------------------------ecbd3787f083e4b1
>> Content-Disposition: form-data; name="entity_document"
>> 
>> {
>>     "FolderId": "a0a1300000ZG7u3",
>>     "Name": "test.png"
>>   }
>> --------------------------ecbd3787f083e4b1
>> Content-Disposition: form-data; name="Body"; filename="file151001ac96950"
>> Content-Type: image/png
>> 

>> ‰PNG
>> 

>> 
>> --------------------------ecbd3787f083e4b1--

content(req)

[[1]]
[[1]]$message
[1] "Multipart message must include a non-binary part"

[[1]]$errorCode
[1] "INVALID_MULTIPART_REQUEST"

深入研究细节,在我看来 curl 如果我使用upload_file读取并发布元数据文件作为二进制文件,或者发布没有内容类型的json,如果我发布的话它作为角色。这是唯一的问题吗?如果是这样,有没有办法修改处理程序以接受内容类型?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

这是一个(临时)解决方案,基于@ Jeroen对curl的最新补充(参见Github issue以供参考):

library(curl) #>=2.4 on Cran
library(httr)
library(jsonlite)

url <- "https://[Salesforce Instance]/services/data/v39.0/sobjects/Document/"
header <- add_headers(c(Authorization = "Bearer [Salesforce Key]"))

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

media <- upload_file(path)

x  <- data.frame(Name=basename(path),ParentId="[Salesforce Parent ID]",Description="",IsPrivate=FALSE)
x <- as.character(toJSON(unbox(x),pretty=T))
metadata <- form_data(x,"application/json") #new curl function to handle a character body with defined type

body <- list(metadata=metadata,Body=media)

req <- httr:::request(fields=body)
req <- httr:::request_build("POST",url,req,header)

response <- httr:::request_perform(req,new_handle())

我为httr添加request以合并这个新处理,并且可以想象一旦它们使用它将更简单易用,即简单POST(url,header,body=body)

希望它有所帮助!

答案 1 :(得分:0)

这是OP试图做的一个完整而可行的示例。

library(httr)

# JSON formatted metadata
x <- list(a = 1, b = 2)

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

# Construct multipart body
#   Note the use of form_data() and form_file() which allow 
#    the user to specify the content type of each part
body <- list(
  metadata = curl::form_data(toJSON(x), type = "application/json"),
  media = curl::form_file(media, type = mime::guess_type(media))
)

POST(url, body, encode = "multipart")