title: Powershell web request - 400 Bad Request
body: <p>Im building a powershell script to execute commands on a REST-like server, I am using powershell to POST XML data to the webservice. Everything is working perfectly except when the "application" produces an error. The application replies back with details in xml and also gives me a Web Request Error (in this case 400) which is when i have problems. Whenever i get a Web Request Error that is not 200 the commands throw an error which screws up how i handle the error.</p>
  
  <p>This is an example of the response im receiving (using fiddler):</p>
  
  <pre><code>HTTP/1.1 400 Bad Request
  Date: Thu, 29 Nov 2012 04:29:48 GMT
  Content-Type: text/xml;charset=ISO-8859-1
  Connection: close
  
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;Status&gt;
      &lt;operation&gt;Add Network&lt;/operation&gt;
      &lt;result&gt;ERROR&lt;/result&gt;
      &lt;resultCode&gt;REASON_210&lt;/resultCode&gt;
  &lt;/Status&gt;
  </code></pre>
  
  <p>This is the error i receive in powershell:</p>
  
  <pre><code>Exception calling "UploadData" with "3" argument(s): "The remote server returned an error: (400) Bad Request."
  At C:\Scripting\test.ps1:47 char:34
  +     $response = $query.UploadData &lt;&lt;&lt;&lt; ($url,"POST",$byteArray)
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException
  
  Exception calling "GetString" with "1" argument(s): "Array cannot be null.
  Parameter name: bytes"
  At C:\Scripting\test.ps1:48 char:62
  +     $stringresponse = [System.Text.Encoding]::ASCII.GetString &lt;&lt;&lt;&lt; ($response)
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException
  </code></pre>
  
  <p>I dug further into the error and this is as close as i could get to retrieving the content of the response (the XML).</p>
  
  <p>I thought I might be able to pull it out from this..</p>
  
  <pre><code>$error[1].exception.InnerException.Data
  </code></pre>
  
  <p>But it is empty :(</p>
  
  <pre><code>PS C:\Scripting&gt; $error[1].exception.InnerException | fl -force
  
  
  Status         : ProtocolError
  Response       : System.Net.HttpWebResponse
  Message        : The remote server returned an error: (400) Bad Request.
  Data           : {}
  InnerException :
  TargetSite     : Byte[] UploadDataInternal(System.Uri, System.String, Byte[], System.Net.WebRequest ByRef)
  StackTrace     :    at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest&amp; request)
                      at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
                      at System.Net.WebClient.UploadData(String address, String method, Byte[] data)
                      at UploadData(Object , Object[] )
                      at System.Management.Automation.MethodInformation.Invoke(Object target, Object[] arguments)
                      at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[]
                   originalArguments)
  HelpLink       :
  Source         : System
  
  
  PS C:\Scripting&gt; $error[1].exception.InnerException.response | fl
  
  
  IsMutuallyAuthenticated : False
  Cookies                 : {}
  Headers                 : {Connection, Transfer-Encoding, Content-Type, Date}
  ContentLength           : -1
  ContentEncoding         :
  ContentType             : text/xml;charset=ISO-8859-1
  CharacterSet            : ISO-8859-1
  Server                  :
  LastModified            : 29/11/12 4:04:19 PM
  StatusCode              : BadRequest
  StatusDescription       : Bad Request
  ProtocolVersion         : 1.1
  ResponseUri             : https://api-au.dimensiondata.com/oec/0.9/cdefb891-1552-4008-8638-7393eda8d7e1/network
  Method                  : POST
  IsFromCache             : False
  </code></pre>
  
  <p>Here is my powershell code that I am using:</p>
  
  <pre><code>function send_web ($url,$data) {
      [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
      $query = new-object system.net.WebClient
      $auth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))
      $query.Headers.Add('Authorization', $auth )
      [byte[]]$byteArray = [System.Text.Encoding]::ASCII.GetBytes($data) 
      $query.Headers.Add("ContentType","application/xml")
      $response = $query.UploadData($url,"POST",$byteArray)
      $stringresponse = [System.Text.Encoding]::ASCII.GetString($response)
      return $stringresponse
  }
  </code></pre>
  
  <p>This seems like it shouldn't be a hard thing to do any help would be much appreciated :)</p>
  
  <p>Richard</p>