Invoke-WebRequest, POST failing with (400) Bad Request


hello everyone,

when run following statement, throws me 400 bad request error on production environment running fine on local environment. $uri (url) different in both cases doing same task.

invoke-webrequest -usebasicparsing  -header $headers -contenttype 'application/xml' -method post -body $postparams -uri $uri

it returns 400 bad request

invoke-webrequest : remote server returned error: (400) bad request.
@ 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

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
}

returned following result

### inside catch ###
errormessage:
remote server returned 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 log more details</text></message></messages></responsestatus></response>

above exception results not providing relevant info.

same invoke-webrequest call working fine on 1 server failing on server above errors.

$headers authorization token.
$postparams <request></request>
$uri url

first call below, made url retrieve authentication token completing successfully.

invoke-webrequest -uri $authenticationurl

next post call failing.

might reasons error on respective server?

seems me, code fine wrong while making connection production server.but not sure troubleshooting steps need follow?

in advance.

post here tricky iis issues http://forums.iis.net


\_(ツ)_/



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Edit Group Policy

Hyper-V VM not reaching OS 'Logon' screen

DNS question...