0
votes

I'm trying to create a Rest API's from a power shell, when i'm trying to run the script i'm getting the following error.

I'll put the script and the error also. Thank you so much !

Script:

#Skip SSL Errors
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
                return true;
            }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy


$url = 'http://github.fuze.org/api/Lists/GetList'
$acctname = 'administartor'
$password = 'M00nd4t'
$ContentType = 'application/json'
$body = @"
    {
"faceid": "431494039483185",
"Id": "7647484953",
"r": "1444",
"session":=""
}
"@
$encodedCredentials =  [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)"))

$headers = @{ 
'Authorization' = "Basic $encodedCredentials"
'Accept' = 'application/json'
'Content-Type' = 'application/json'
 }


$var = invoke-restmethod -Uri $url -Method POST -Body $body -Headers $headers -ContentType "application/json" -UseBasicParsing

Headers:

Name                           Value                                                                                                                                       
----                           -----                                                                                                                                       
Contenttype                    appliaction/json                                                                                                                            
Accept                         application/json                                                                                                                            
Authorization                  Basic XXX=     

Error:

invoke-restmethod : The remote server returned an error: (400) Bad Request.
At line:37 char:8
+ $var = invoke-restmethod -Uri $url -Method POST -Body $body -Headers  ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Command

1

1 Answers

0
votes

Bad request errors generally mean that the payload being sent is of in-correct format. Try changing body to:

$body = @'
{
"faceid": "431494039483185",
"Id": "7647484953",
"r": "1444",
"session":""
}
'@

Check out the types of HTTP return codes.