3

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.

Dev
  • 63
  • 1
  • 7
  • 1
    you should show the contents of your variables at the time of submitting, HTTP 400 means the request is malformed, most likely one of your variables contains data in an invalid format – Paul Jan 21 '17 at 09:36
  • 1
    Agree with Paul, can you post the contents of $postParams? – Simon Catlin Jan 21 '17 at 11:48
  • Thanks Paul and Simon for your comments. Here is some more information on variables. >$headers is authorization token. >$postParams is . >$uri is url . As I mentioned earlier, same call is working fine on my local environment, with same $postParams. Even the first call below, which I made to URL for authentication token is completing successfully. Invoke-WebRequest -URi $authenticationURL . But Next POST call is failing. Appreciate your comments/suggestions. – Dev Jan 21 '17 at 20:10
  • Also tried same call with curl instead of Invoke-WebRequest. Same issue, it worked fine on local environment, but failed on production environment. – Dev Jan 21 '17 at 20:12
  • Sounds like your server is not configured properly to me. Either the process accepting HTTP requests doesn't know how to route the data, or there is an error with the authorization process I would guess? Have you verified the server-side paths (virtual or non-virtual)? – PSGuy Jan 22 '17 at 01:31
  • Sounds like a server restriction (eg. unauthorised data or data doesn't exist) or protocol restriction (eg. Freddy isn't allowed to the party) – Zimba May 08 '20 at 16:43

0 Answers0