Achieving HTTP PUT with JUCE URL class

I’ve been having a few problems getting HTTP PUT to work with the URL class and thought the magic combination of options may be useful for others.
If however I’m just plain doing it wrong I’d appreciate you letting me know too :slight_smile:

When using PUT there are typically URL parameters e.g. if I’m sending a file to S3 I would PUT to:
https://ecap.s3.eu-central-1.amazonaws.com/stg/SomelongUuidyPath/filename.ext?X-Amz-Expires=259200&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=etc...

The working code:

URL upload_url = URL::createWithoutParsing (uploadUrlStringIncludingParams);
MemoryBlock block;
file.loadFileAsData (block);
URL upload_with_data = upload_url.withPOSTData (block);

ScopedPointer<InputStream> is = upload_with_data.createInputStream (true, nullptr, nullptr, {}, 10000, &response_headers, &status_code, 5, "PUT");

Gotchas:
If you set createInputStream's first parameter DoLikePostRequest:bool to false (it’s a PUT not a POST after all), then it appears to have worked successfully (I got a 200 OK code) but it has PUT 0 bytes on the server.

Setting DoLikePostRequest to true fixes that but you also need ot use URL::createWithoutParsing otherwise the url params are stripped off.

Whilst it would be better to not have the URL class try to treat URL params and POST body params as the same thing, I realise the API is what it is now. But perhaps if you add withBodyData(MemBlock / String), and send that data for other HTTP methods regardless of what DoLikePostRequest is set to, that would help?

Ta,
Dave

2 Likes