API method POST JSON data gives response code 200, but content length 0

Hi @adam
The API endpoint with method POST contains: $body without authentication

Invoke-RestMethod "URL_of_the_API" -Method Post -Body “{‘username’: ‘adam’}” -ContentType 'application/json'

After invoking I can see following line in the log: Status: 200 Content-Type: application/json Length: 0 and there is no data in the API posted.

I’m new to this and wondering why there is no date posted!

Product: PowerShell Universal
Version: 4.2.14
  • First, as you have it written, your syntax is invalid. You’re using rather than ', so the command fails to run.

    • If you correct your syntax and make it Invoke-RestMethod "URL _of_the_API" -Method Post -Body “{'username': 'adam'}” -ContentType 'application/json' it will succeed and you get data.
  • Second, your script in your API (as you’re saying you’ve written it) is not doing anything. You’re not outputting the $body variable to anything, like the console.

    • If you’re just testing, you could do something like Write-Output $body and see that it is working correctly.

Hi @Jesse.Peden

  1. Still the length is 0

  2. I see the data in the API and in the PSU console using the below example:

    $body = "{'username':'adam'}"
    Write-Output $body
    

but when Invoke-RestMethod then there is no data posted.

Unless I’m misunderstanding your setup, this is my replication of your API as well as testing it from a workstation.

API (Endpoint):

New-PSUEndpoint -Url "/testing" -Method @('POST') -Endpoint {
    Write-Output $body
}

Command to test with:

Invoke-RestMethod "https://<redacted>/testing" -Method Post -Body “{'username': 'adam'}” -ContentType 'application/json'

Result on the workstation:

username
--------
adam

Log output in PSU:
[4/5/2024 8:11:19 AM] [Information] [Api-4] Status: 200 Content-Type: application/json Length: 20

If there is something different in my test setup, please let me know what to change, to better replicate your setup.

Indeed, the setup is exactly the same, and I’m receiving the same result on my workstation—but no data in the browser when using the API address.

FYI: PSU running as service with dedicated service account.

Log output in the PSU:
Fri, 05 Apr 2024 13:52:59 GMT Information Status: 200 Content-Type: application/json Length:0

Syslog:

[INF] Executing endpoint '/notificationhub'
[INF] Request starting HTTP/1.1 POST http://xx:5000/testpostapi application/json 20
[INF] Request finished HTTP/1.1 POST http://xx:5000/testpostapi application/json 20 - 200 - application/json 19.8792ms
[INF] Executed endpoint '/notificationhub'
[INF] Request finished HTTP/1.1 GET http://xx:5000/notificationhub?id=PvTls05aYhlAJC6ONRfDYA - - - 101 - - 119990.6750ms
[INF] Request starting HTTP/1.1 POST http://xx:5000/notificationhub/negotiate?negotiateVersion=1 text/plain;charset=UTF-8 0
[INF] CORS policy execution failed.
[INF] Request origin http://xx:5000 does not have permission to access the resource.
[INF] Executing endpoint '/notificationhub/negotiate'
[INF] Executed endpoint '/notificationhub/negotiate'
[INF] Request finished HTTP/1.1 POST http://xx:5000/notificationhub/negotiate?negotiateVersion=1 text/plain;charset=UTF-8 0 - 200 - application/json 3.3514ms
[INF] Request starting HTTP/1.1 GET http://xx:5000/notificationhub?id=4QzJWTK1PH79RXdr6yL8CA - -
[INF] CORS policy execution failed.
[INF] Request origin http://xx:5000 does not have permission to access the resource.
[INF] Executing endpoint '/notificationhub'

I’m not sure. Based on your logs, it looks like there’s 20 bytes of data.

[INF] Request starting HTTP/1.1 POST http://xx:5000/testpostapi application/json 20
[INF] Request finished HTTP/1.1 POST http://xx:5000/testpostapi application/json 20 - 200 - application/json 19.8792ms

Maybe one of the other users here will have an idea or know if this is some sort of PSU bug.

Okay, thank you for checking this out.

I do still have problem with this.

  • Upgraded to PSU 4.2.18

  • Changed from http to https

  • Tried also without service account for the PSU.

  • Changed the API to GET and POST method. With the code get-random and $body the result in the web GUI show following data: [870089854,""] however noting posted when running this

     Invoke-RestMethod "https://<redacted>/testing" -Method Post -Body “{'username': 'adam'}” - 
     ContentType 'application/json'
    

I’m still at a loss as to why my own tested gave the expected result but your setup (which is theoretically “identical”) is giving a different result.

I was hoping another user on here would chime in with some ideas. I’ll make the API again and do some more testing to see if there’s any change in 4.2.18 on my end.

I recreated the API and I’m still not able to replicate any issues.

PS C:\Users\<redacted>> Invoke-RestMethod "https://<redacted>/testing" -Method Post -Body “{'username': 'adam'}” -ContentType 'application/json' -verbose
VERBOSE: Requested HTTP/1.1 POST with 20-byte payload
VERBOSE: Received HTTP/1.1 response of content type application/json of unknown size
VERBOSE: Content encoding: utf-8

username
--------
adam

brave_FuM50Xg1VR

Maybe @adam has ideas on why our results are different from each other.

Can you post what code your API endpoint script is running? I was able to replicate what @Jesse.Peden did and it was successful on 4.2.17 with no issue.

The API endpoint just have code $body right now. I have also tried following code:

$body = "{'username':'adam'}"
Write-Output $body

Here is the result from invoke:

Invoke-RestMethod https://xxx/testing -Method Post -body  "{'username': 'adam'}" -ContentType "application/json" -Verbose

VERBOSE: POST https://xxx/testing with -1-byte payload
VERBOSE: received -1-byte response of content type application/json

username
--------
adam

@Jesse.Peden @turnitoff_onagain : The server using TLS 1.2 so this could lead to unexpected behavior such as receiving a -1-byte payload response (not sure) ? Can you guys test with TLS 1.2 ?

@PSKA_90 I’ll have to look into it when I get to the office, to make sure it won’t break anything by forcing TLS 1.2 globally.

Alternatively, you can add -SslProtocol <Tls, Tls11, Tls12, or Tls13> to the Invoke-RestMethod command to force a different version for just that command, if you want to try that from your end to see if it works by setting it to use TLS 1.2. Invoke-RestMethod and Invoke-WebRequest use TLS 1.0 by default, so if you’re only allowing TLS 1.2 globally, that could very well be the issue.

If you can try the command Invoke-RestMethod "https://<redacted>/testing" -Method Post -Body “{'username': 'adam'}” -ContentType 'application/json' -verbose -SslProtocol Tls12 and share the result, that might help.

Thank you for testing this @Jesse.Peden

We have GPO to disables old SSL and TLS version 1.x
SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.x\Server

The appsettings.json and the .net registry was not configured with TLS 1.2. I just updated like your config above.

Still same results

VERBOSE: POST https://xxx/testing with -1-byte payload
VERBOSE: received -1-byte response of content type application/json

username
--------
adam

From Powershell 7, i get this results:

VERBOSE: POST with 20-byte payload
VERBOSE: received -byte response of content type application/json
VERBOSE: Content encoding: utf-8

username
--------
adam

No data posted on the API.

I just want to make sure that this is not related to TLS. Yes, agree. I think this is something else.

That is still so weird.

In your API, try adding the line Write-Information $body to see if you then see a log entry containing the data inside of PSU.

From PSU log:
Information Status: 200 Content-Type: application/json Length: 2
Mon, 22 Apr 2024 06:31:27 GMT Information {'username': 'adam'}

Web browser show this when accessing the API URL (after and before Invoke-RestMethod:
[]

Note:The server log (programdata) on the server shows the right time, however the dashboard log shows -2 hours from my server time, not sure this has anything do with this issue.

I tried also to execute the Invoke-RestMethod https://xx/testing -Method Post -body "{'username': 'adam'}" -ContentType "application/json" -Verbose: from PSU scripts and can see there is no data in the pipeline:

That’s a separate known bug they’re fixing in 4.2.19 already. The time offset seems random. For me, it’s off by 1 hour, and others it’s been off by 2, 7, 9, etc.