0

I am trying to add HTML as a value in JSON body in below code in PowerShell. It gives 400 Bad Request. The HTML content contains lots of ampersands, quotes, commas etc. due to which this fails. The same code works if I used simple strings instead of web page HTMLs. Is there a way to make this work with web page HTMLs as value in JSON body-

$headers=@{}
$headers.Add("Content-Type", "application/json")
$headers.Add("X-Auth-Email", "")
$response = Invoke-RestMethod -Uri 'https://api.cloudflare.com/client/v4/accounts/account_identifier/storage/kv/namespaces/namespace_identifier/bulk' -Method PUT -Headers $headers -ContentType 'application/json' -Body '[
  {        
    "key": "My-Key",
    "value": "Very long HTML of a web page"
  }
]'
Ghanendra
  • 341
  • 2
  • 13
  • actually, you can use an here string or escape the string before sending it, but it's very unusual to send an html inside json as a parameter, can you share the api doc for this action? – Avshalom Apr 24 '23 at 10:51
  • If you want to ensure your json is valid you could try ```$body = @( @{ "key" = "My-Key"; "value" = "Very long HTML of a web page" } ) | ConvertTo-Json```. That will escape and encode special characters properly, and then you can use the variable in your call like this ```Invoke-RestMethod ... -Body $body``` – mclayton Apr 24 '23 at 13:00

0 Answers0