Invoke-WebRequest inside API endpoint

Product: PowerShell Universal
Version: 2.5.3
Docker image: ironmansoftware/universal:2.5.3-ubuntu-20.04

I am trying to learn PowerShell universal, so it could be that it’s me doing something wrong, but:
If i will do API endpoint for POST with source:


# Enter your script to process requests.
$newHeaders = @{'Content-Type'='application/json'}

Invoke-WebRequest -Uri 'http://192.168.1.65:9200/greg3/_doc' -Method 'POST' -Headers $newHeaders -Body (@{'points'=90;'date'=get-date -format 'o'}|convertto-json)

Then when i will call it from powershell (it does not matter if its my powershell in my windows, or powershell inside the PowershellUniversal container) i get:

Invoke-WebRequest -Uri 'http://localhost:5000/ttest' -Method Post
Invoke-WebRequest: An item with the same key has already been added. Key: Content

If i will run this from my own powershell terminal on my windows, on my own it works fine as in:

Invoke-Webrequest -Uri 'http://192.168.1.65:9200/greg5/_doc' -Body  (@{'points'=90;'date'=get-date -format 'o'}|convertto-json) -Method 'POST' -Headers @{'Content-Type'='application/json'}`

This works fine.
Then i did another test with the endpoint and i used Invoke-RestMethod instead of Invoke-WebRequest

$newHeaders = @{'Content-Type'='application/json'}
Invoke-RestMethod -Uri 'http://192.168.1.65:9200/greg5/_doc' -Body  (@{'points'=90;'date'=get-date -format 'o'}|convertto-json) -Method 'POST' -Headers @{'Content-Type'='application/json'}

And when i use my powershell to call this api endpoint everything works just fine.

Could anyone tell me what i am doing wrong with my first api call ?

When i run exactly the same lines inside a PowershellUniversal Script , this works just fine:


Screenshots from running it via API endpoint


I have no idea why i can’t push my own headers for invoke-webrequest. If i would drop the -Headers … then the command works(but it failes on the Elastic api side, as it will not accept that request as the header is missing, but the command itself does not produce that error)

I gotta admit, I have no idea why this isn’t working. I might need to try something similar myself to provide some useful guidance. All I gotta say is this is super weird and not expected so I don’t think you are doing anything wrong.

Thank you Adam.

same problem here, i got an 400 Error
“Error: Request failed with status code 400” via Endpoint.
If i run it as Script (same environment) i got the data from Invoke-WebRequest.

scary… if i try it with Invoke-RestMethod via Endpoint it is working…