I’m creating an object for making HTTP requests like this:
$!ua = Cro::HTTP::Client.new(
base-uri => 'https://what.ever/api/',
content-type => 'application/json'
);
but one of the API endpoints requires using content-type: multipart/form-data
instead.
Naively doing something like
my $response = await $!ua.post(
"add",
content-type => 'multipart/form-data',
body => [Cro::HTTP::Body::MultiPartFormData::Part.new(
headers => [Cro::HTTP::Header.new(
name => 'content-type',
value => $mimetype
)],
name => $name,
body-blob => $data,
)]
);
results in an error because both content types are concatenated:
Could not parse media type 'application/json,multipart/form-data'
But how can I override the default content type here?
I’ve tried doing
$!ua.content-type = '';
LEAVE {
$!ua.content-type = 'application/json';
}
in the same function but this fails with
Cannot modify an immutable Str (application/json)
which I don’t fully understand (why assigning an empty string works but not for the non-empty one?), but I guess this is not the right way to do it anyhow. But which one is?
I can just create another client, of course, but this seems a bit wasteful. And I can avoid specifying the default content type, but then I’d have to do it in all the other requests (of which there are a few), which is not great neither.