Error: Invoke-RestMethod: Bad request (400): Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
K
 
Zeile 10: Zeile 10:
  
 
== Cause ==
 
== Cause ==
''If you use umlauts in a Invoke-RestMethod with JSON then the error can occurs''
+
''If you use umlauts in a Invoke-RestMethod with JSON then the error can occurs'' <br>
 
For example: <br>
 
For example: <br>
 
<source lang="PowerShell">''Invoke-RestMethod -UseBasicParsing -Uri "https://YourURL" -Headers $header -Body $JSON -ContentType application/json -Method Post'' </source>
 
<source lang="PowerShell">''Invoke-RestMethod -UseBasicParsing -Uri "https://YourURL" -Headers $header -Body $JSON -ContentType application/json -Method Post'' </source>
Zeile 16: Zeile 16:
  
 
== Solution ==
 
== Solution ==
*Add following encoding to your JSON-Object -> '''([System.Text.Encoding]::UTF8.GetBytes($JSON))'''
+
*Add following encoding to your JSON-Object -> '''([System.Text.Encoding]::UTF8.GetBytes($JSON))''' <br>
 
For example: <br>
 
For example: <br>
 
<source lang="PowerShell">''Invoke-RestMethod -UseBasicParsing -Uri "https://YourURL" -Headers $header -Body ([System.Text.Encoding]::UTF8.GetBytes($JSON)) -ContentType application/json -Method Post'' </source>
 
<source lang="PowerShell">''Invoke-RestMethod -UseBasicParsing -Uri "https://YourURL" -Headers $header -Body ([System.Text.Encoding]::UTF8.GetBytes($JSON)) -ContentType application/json -Method Post'' </source>

Aktuelle Version vom 9. Januar 2019, 13:33 Uhr

Error

Invoke-RestMethod : Der Remoteserver hat einen Fehler zurückgegeben: (400) Ungültige Anforderung.
In Zeile:88 Zeichen:9
+         Invoke-RestMethod -UseBasicParsing -Uri "https://jira.inventx ...
+         13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)13:33, 9. Jan. 2019 (CET)~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


Cause

If you use umlauts in a Invoke-RestMethod with JSON then the error can occurs
For example:

''Invoke-RestMethod -UseBasicParsing -Uri "https://YourURL" -Headers $header -Body $JSON -ContentType application/json -Method Post''


Solution

  • Add following encoding to your JSON-Object -> ([System.Text.Encoding]::UTF8.GetBytes($JSON))

For example:

''Invoke-RestMethod -UseBasicParsing -Uri "https://YourURL" -Headers $header -Body ([System.Text.Encoding]::UTF8.GetBytes($JSON)) -ContentType application/json -Method Post''



More informationen: https://stackoverflow.com/questions/15290185/invoke-webrequest-issue-with-special-characters-in-json