Custom Purge 400 Bad request

I’m trying to use Powershell for a custom URL purge but I get 400 Bad request in return. A full purge is working fine.

Code snippet:
$API =
$PurgeCacheUri = “https://api.cloudflare.com/client/v4/zones/$ZoneId/purge_cache”;
$RequestHeader = @{
‘Authorization’ = “Bearer $API”
};
$UrlsToPurge = “https://example.com
$RequestBody = @{ files = “[$UrlsToPurge]” } | ConvertTo-Json -Compress
Invoke-WebRequest $PurgeCacheUri -Method Delete -ContentType “application/json” -Headers $requestHeader -Body $RequestBody

Why isn’t this working?

Solved with the help of support. It looks like when there is only a single URL, the RequestBody does not contain the right double quotes and brackets. I solved it by changing the code into:

$UrlsToPurge = “https://example.com”,“https://example.com
$RequestBody = @{ files = $UrlsToPurge } | ConvertTo-Json -Compress

And now it’s working fine.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.