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
body_config handling for new curl::form_data function #430
Comments
Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session. |
Sorry, this still isn't exactly reproducible since I have to hide my authorization and you'd need an active Salesforce account, but if I knew of a public API that had this upload formatting, I'd use it. Anyway here's what reprex produced (didn't know about this package, but it's quite helpful!): url <- "https://my_salesforce_instance/services/data/v39.0/sobjects/Attachment/"
header <- httr::add_headers(c(Authorization="my_salesforce_auth"))
x <- data.frame(Name="test1.png",ParentId="a031300000GjJj0")
json <- jsonlite::toJSON(jsonlite::unbox(x),pretty=T)
metadata <- curl:::form_data(as.character(json),"application/json")
tmp <- tempfile(fileext=".png")
png(tmp)
plot(mtcars)
dev.off()
#> png
#> 2
media <- httr::upload_file(tmp)
body <- list(Info=metadata,Body=media)
resp <- httr::POST(url,header,body=body,encode="multipart")
#> Warning in charToRaw(enc2utf8(val)): argument should be a character vector of length 1
#> all but the first element will be ignored
resp
#> Response [https://my_salesforce_instance/services/data/v39.0/sobjects/Attachment/]
#> Date: 2017-07-24 14:04
#> Status: 400
#> Content-Type: application/json;charset=UTF-8
#> Size: 104 B
handle <- curl::new_handle()
req <- httr:::request(fields=body)
req <- httr:::request_build("POST",url,req,header)
resp <- httr:::request_perform(req,handle)
resp
#> Response [https://my_salesforce_instance/services/data/v39.0/sobjects/Attachment/]
#> Date: 2017-07-24 14:04
#> Status: 201
#> Content-Type: application/json;charset=UTF-8
#> Size: 54 B
httr::content(resp)
#> $id
#> [1] "00P18000001DAIkEAO"
#>
#> $success
#> [1] TRUE
#>
#> $errors
#> list() |
The latest version of OpenCPU supports form data as well to post binary payloads. Perhaps you can use this as a prototype. # Post requests with various form-data types
data_arg <- protolite::serialize_pb(cars)
geom_arg <- jsonlite::toJSON(c("point", "smooth"))
h <- curl::handle_setform(new_handle(), x = 'speed', y = 'dist',
data = curl::form_data(data_arg, type = 'application/protobuf'),
geom = curl::form_data(geom_arg, type = 'application/json'))
req <- curl::curl_fetch_memory('https://cran.ocpu.io/ggplot2::qplot', handle = h)
# Get the output
url <- paste0(curl::parse_headers_list(req$headers)$location, "graphics/1/pb")
req <- curl::curl_fetch_memory(url)
reqplot <- protolite::unserialize_pb(req$content)
print(reqplot) |
I think this illustrates the basic problem: citation <- upload_file(system.file("CITATION"))
POST("https://httpbin.org",
body = list(
string = curl::form_data('{"a": 1}', type = "application/json"),
file = upload_file(system.file("CITATION"))
),
verbose()
) |
@jeroen is there a good way to test this? When posting to httpbin, I see:
which is ok, I think. But the returned json has
|
The fix is simple, as @ampu3ro it's just a matter of switching from @jeroen would you consider adding support for other atomic vectors in |
@hadley I think you just need to add another |
@jeroen for multipart, it's here: https://github.com/r-lib/httr/blob/master/R/body.R#L58 |
How does that not break |
Well it needs a fix there too - but you can also have a list, for |
Well maybe, does it make sense to send a single unnamed |
No, both |
httr allows you to use |
Right you probably shouldn't have used the Anyway that ship has probably sailed. The important thing is to not touch the |
Right, so would you consider coercing numeric and logical vectors automatically? I'd prefer not to make an API change in a widely used package (even though using things other than character vectors is likely to be rare) |
Can you just do this? https://github.com/jeroen/httr/commit/a112098264e75e3c43279ed1d4965dc2f1522185 |
It just seems easier to rely on curl to do all the coercion, so that there's no need to make changes in the future - whatever curl supports, httr will support. |
Hmm I prefer It seems the fix is easy on the httr side without need to change the api at all? |
Sure, if you don't want to coerce, it's fine. I can do it. |
This API will be completely reworked in httr2. |
When create the fields for a multipart body request. Fixes r-lib#430.
@jeroenooms added a useful new field type for multipart forms that addresses the issue of defining content-type for character data. The
form_data()
function handles this nicely, but outputs a list withvalue
of class "raw" andtype
of class "character". In order to play nice withhttr
, I thinkbody_config()
needs to be modified to not convert raw to character (line 58 here)Say I want to
POST
this formIdeally I could write something like
but currently only this works
The text was updated successfully, but these errors were encountered: