简体   繁体   中英

multipart/form-data with REST::Client in Perl

How do I use REST::Client to send a 'multipart/form-data' request to a REST API? The API in question is here , with this particular request being the problematic one. With other post requests, I can simply send data as JSON, using

my $params = { api_key => $key, match_attachments => { url => "http://example.com" } };
$client->POST("/tournaments/$tid/matches/$mid/attachments.json", to_json($params), { "Content-Type" => "application/json" });

However, as this could possibly expect a file, it appears I have to use 'multipart/form-data' as the Content-Type. I've tried numerous things, such as sending the data as JSON (which doesn't pick up the api_key), using REST::Client::buildQuery (which doesn't pick up the 'match_attachments'), and sending hash and arrayrefs, both of which had the module complain that it needs a scalar ref. How do I correctly format the data so it will go through? Is it the same process with a file as a plaintext url?

It seems that REST::Client doesn't support content in RFC2388 (multipart/formdata) which the API may require.

There will be two distinct content blocks - the parameters JSON, and the encoded binary data for the image.

The 'curl' command can do this with the -F option, so you might want to try this first.

A bit late but maybe submit this as a feature request: https://github.com/milescrawford/cpan-rest-client/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM