Hello Everyone,
When I run following Statement, it throws me 400 Bad Request error on production environment but is running fine on my local environment. $uri (url) is different in both cases but doing same task.
Invoke-WebRequest -UseBasicParsing -Header $headers -ContentType 'Application/xml' -METHOD POST -BODY $postParams -Uri $uri
It returns 400 Bad Request
Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At C:\script.ps1:107 char:14
+ ... resultRun = Invoke-WebRequest -Header $headers -ContentType 'Applicat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I tried catching exception, using below:
try {
Invoke-WebRequest -UseBasicParsing -Header $headers -ContentType 'Application/xml' -METHOD POST -BODY $postParams -Uri $uri
}
catch {
echo '### Inside catch ###'
$ErrorMessage = $_.Exception.Message
echo '## ErrorMessage ##' $ErrorMessage
$FailedItem = $_.Exception.ItemName
echo '## FailedItem ##' $FailedItem
$result = $_.Exception.Response.GetResponseStream()
echo '## result2 ##' $result
$reader = New-Object System.IO.StreamReader($result)
echo '## reader ##' $reader
$responseBody = $reader.ReadToEnd();
echo '## responseBody ##' $responseBody
}
This returned following result
### Inside catch ###
ErrorMessage:
The remote server returned an error: (400) Bad Request.
FailedItem :
result2:
CanTimeout : True
ReadTimeout : -1
WriteTimeout : -1
CanRead : True
CanSeek : True
CanWrite : True
Capacity : 286
Length : 286
Position : 0
reader :
CurrentEncoding : System.Text.UTF8Encoding
BaseStream : System.Net.SyncMemoryStream
EndOfStream : False
responseBody:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Response><ResponseStatus><Status>FAILURE</Status><Messages><Message><Code>400</Code><Type>INVALID DATA</Type><Text>Please see the log for more details</Text></Message></Messages></ResponseStatus></Response>
Above exception results not providing relevant info.
Same Invoke-WebRequest call is working perfectly fine on one server but is failing on another server with above errors.
What might be the reasons for this error on the respective server?
It seems to me, that code is fine but something wrong while making connection with production server.But not sure what troubleshooting steps we need to follow?
Thanks in advance.