Home > OS >  Powershell Curl alternative with Multiform data in Body getting 400 BAD request
Powershell Curl alternative with Multiform data in Body getting 400 BAD request

Time:06-17

I have a curl command that i translated in to powershell but i get Below error

ErrorMessage

The remote server returned an error: (400) Bad Request. CanTimeout : True ReadTimeout : -1 WriteTimeout : -1 CanRead : True CanSeek : True CanWrite : True Capacity : 256 Length : 65 Position : 0 CurrentEncoding : System.Text.UTF8Encoding BaseStream : System.Net.SyncMemoryStream EndOfStream : False {"status":400,"message":"invalid request parameter name or type"}

   'X-ANYPNT-ENV-ID' =  '4a96af5237cf1b64'
   'X-ANYPNT-ORG-ID' = 'c6583b9fd79234ad'
   'Authorization' = 'Bearer a2b6e09a-5e033d4c46c'
}

# Body section

$Body = @{
"file" = "@/E:/actions-runner/workspace/target/sapi-ecm-documents-v1-1.0.2.jar"
"appInfoJson" = @(
  @{ 
    "domain"                = "testfi-fi-v1-proxy-test" 
    "muleVersion"           = @{"version" = "4.4.0"}
    "properties"            = @{"anypoint.platform.client_id" = "8f95f0f5c6b6"; "secure.key" = "sYf%NslNRH*D$@"; "env" = "dev"; "anypoint.platform.client_secret" = "acF0D364be7a"}
    "propertiesOptions"     = @{"secure.key" = @{"secure" = "true" }; "anypoint.platform.client_secret"= @{"secure" = "true" }}
    "region"                = "us-west-1"
    "monitoringEnabled"     = "true"
    "monitoringAutoRestart" = "true"
    "workers"               = @{"amount" = "1" ; "type" = @{"name" = "Micro"; "weight" = "0.1" ; "cpu" = "0.1 vCores"; "memory" = "500 MB memory"}}
    "loggingNgEnabled"      = "true"
    "persistentQueues"      = "true"
   }
  )
"autoStart" = "true" 
}

$Body = $Body | ConvertTo-Json -Depth 5

try {
$result = Invoke-WebRequest -Method Post -Uri "https://anypoint.mulesoft.com/cloudhub/api/v2/applications" -Headers $Header -Body $Body -ContentType "application/json"
echo $result.domain
echo $result.muleVersion
echo $result.properties
}
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
}```

**File Body should have**
file = <filepath>
autostart="true"
appInfoJson={
"domain": "testfi-fi-v1-proxy-test",
"muleVersion": {
"version": "4.4.0"
},
"properties": {
"anypoint.platform.client_id": "8f959f0f5c6b6",
"secure.key": "sYf%NH*D$@",
"env": "dev",
"anypoint.platform.client_secret": "acF0EF364be7a"
},
"propertiesOptions": {
"secure.key": {
"secure": true
},
"anypoint.platform.client_secret": {
"secure": true
}
},
"region": "us-west-1",
"monitoringEnabled": true,
"monitoringAutoRestart": true,
"workers": {
"amount": 1,
"type": {
"name": "Micro",
"weight": 0.1,
"cpu": "0.1 vCores",
"memory": "500 MB memory"
}
},
"loggingNgEnabled": true,
"persistentQueues": true
}

CodePudding user response:

If you output the JSON-ified $Body the output does not match your specification:

PS C:\> $Body = $Body | ConvertTo-Json -Depth 5

PS C:\> Write-Host $Body
{
    "autoStart":  "true",
    "file":  "@/E:/actions-runner/workspace/target/sapi-ecm-documents-v1-1.0.2.jar",
    "appInfoJson":  [
                        {
                            "domain":  "testfi-fi-v1-proxy-test",
                            "monitoringAutoRestart":  "true",
                            "loggingNgEnabled":  "true",
                            "region":  "us-west-1",
                            "monitoringEnabled":  "true",
                            "muleVersion":  {
                                                "version":  "4.4.0"
                                            },
                            "propertiesOptions":  {
                                                      "secure.key":  {
                                                                         "secure":  "true"
                                                                     },
                                                      "anypoint.platform.client_secret":  {
                                                                                              "secure":  "true"
                                                                                          }
                                                  },
                            "persistentQueues":  "true",
                            "workers":  {
                                            "type":  {
                                                         "memory":  "500 MB memory",
                                                         "weight":  "0.1",
                                                         "cpu":  "0.1 vCores",
                                                         "name":  "Micro"
                                                     },
                                            "amount":  "1"
                                        },
                            "properties":  {
                                               "env":  "dev",
                                               "anypoint.platform.client_secret":  
"XXXXXXXXXXXX",
                                               "secure.key":  "XXXXXXXXXXXXXX",
                                               "anypoint.platform.client_id":  "8f95f0f5c6b6"
                                           }
                        }
                    ]
}

Try replacing "true" for $true, and removing quotes around integers and floating point values when you first define $Body.

CodePudding user response:

Below is what worked for me

$path = 'E:\actions-runner\ocuments-v1-1.0.2.jar';
$jsonpath='E:\curljson.json'
$Headers = @{
   'X-ANYPNT-ENV-ID' =  '4a9cf1b64'
   'X-ANYPNT-ORG-ID' = 'c6583234ad'
   'Authorization' =  'Bearer 23redfy23w2'
}
$Uri = 'https://anypoint.mulesoft.com/cloudhub/api/v2/applications'

$fileBytes = [System.IO.File]::ReadAllBytes($path);
$fileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($fileBytes);

$jsonfile = [System.IO.File]::ReadAllBytes($jsonpath)
$jsonEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($jsonfile);

$boundary = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";

$bodyLines = ( 
    "--$boundary",
    "Content-Disposition: form-data; name=`"autoStart`"$LF",
    "true$LF",
    "--$boundary",
    "Content-Disposition: form-data; name=`"appInfoJson`"; filename=`"$jsonpath`"",
    "Content-Type: application/json$LF",
    $jsonEnc,    
    "--$boundary",
    "Content-Disposition: form-data; name=`"file`"; filename=`"$path`"",
    "Content-Type: application/octet-stream$LF",
    $fileEnc,
    "--$boundary--$LF" 
) -join $LF

Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
  • Related