Send a POST Request using Powershell - powershell

I had to send a POST request to a Cloud Platform including a zipped csv file. At the moment I´m using Postman for this, and it goes all correctly. On the Webserver it is not possible to install Postman, so I have to use Powershell for this. I have a problem to find out how the body is to insert in the script:
The Correct Postman post - on the "Code snippet" - with CURL Postman says i have to make it like this:
curl --location --request POST 'https://api.../v..../xxx.csv' \
--header 'Content-Type: application/octet-stream' \
--header 'Ocp-Apim-Subscription-Key: xxxxxx' \
--data-binary '....../brunico.zip'**
on Postman the Code snippet for Powershell ist not fully described, for the body is written: ...file contents here...
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/octet-stream")
$headers.Add("Ocp-Apim-Subscription-Key", "xxxxxx")
$body = "<file-contents-here>"***
$response = Invoke-RestMethod 'https://api.../v..../xxx.csv' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json**
how do I insert the zip file in the body?

Related

Translate cURL -F (--form) statement to PowerShell

I have a working cURL statement that takes the contents of a .csv file and posts them to a REST endpoint:
curl \
'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys' \
-H 'Authorization: apikey <APIKEY>' \
-F 'csv_file=#<FILENAME>.csv'
I am trying to convert it to PowerShell format, but can't seem to get there.
I am fairly new to APIs, so I've been using the Curl2PS tool for translating cURL commands to PowerShell, and it worked fairly well with other types, but it struggles with the -F (--form) parameter. This is what I get when I input the above cURL statement:
WARNING: The parameter '-F' is not yet supported. Please refer to curl man pages: https://curl.se/docs/manpage.html
Invoke-RestMethod -Method Get -Uri 'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys' -Verbose:$false -Headers #{
'Authorization' = 'apikey <APIKEY>'
}
It basically only does a partial job at converting the statement and skips the -F part completely, leaving it blank.
After some googling I tried using the -Infile method, but I probably did it wrong (again, noob here):
Invoke-RestMethod -Method POST -Uri 'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys'-Verbose -Headers #{
'Authorization' = 'apikey <APIKEY>'
'Content-Type' = 'multipart/form-data'
} -Infile '<FILENAME>.csv'
Unfortunately, I get a Bad Request (400) error.
I tried googling for a solution, but couldn't really find one, so any input here would be much appreciated.

Using GET in PowerShell & Exporting JSON to CSV

I'm using the following CURL command, to read/fetch table data from an API:
curl -X GET \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer *myAccessToken*' \
https://www.myWebsite.com/api/orders
This command/API Call returns a table in JSON format. I need to do two things with this.
[1] I need to run this in powershell. I've tried using the above code, and returns a general syntax error:
A parameter cannot be found that matches parameter name 'X'.
[2] In PowerShell, Have the JSON output converted & saved as a CSV file
Any ideas? Thanks!
You can use Invoke-RestMethod Cmdlet to Sends an HTTP or HTTPS request to a RESTful web service.
$uri = "https://www.myWebsite.com/api/orders"
$headers = #{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer <AccessToken>'
'Accept'= 'application/json'
}
$response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
PowerShell formats the response based to the data type. For JavaScript Object Notation (JSON) or XML, PowerShell converts, or deserializes, the content into [PSCustomObject] objects. So you can select the columns you want to export and pipe it into Export-Csv Cmdlet
$response | Select ColumnName1,ColumnName2 | Export-Csv -Path "filename.csv" -NoTypeInformation

Error on PS script that installs app to Hololens using device portal APIs

I'm trying to develop a PS script that install app package to Hololens via the device portal APIs. Looking for some troubleshooting tips on the 400 BAD REQUEST response.
Here are the references I used :
https://learn.microsoft.com/en-us/windows/mixed-reality/device-portal-api-reference
END POINT /api/app/packagemanager/package (POST)
$user = 'snarain'
$pass = 'snarain'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = #{
Authorization = $basicAuthValue
}
$FilePath = 'C:\Users\snarain\Downloads\My HololensApp.appxbundle'
Invoke-RestMethod -Uri 'http://127.0.0.1:10080/api/app/packagemanager/package?package=My%20HololensApp.appxbundle' -Headers $Headers -Method Post -InFile $FilePath -ContentType 'multipart/form-data'
The output is Invoke-RestMethod : The remote server returned an error: (400) Bad Request. However, I am able to use Postman to hit the end point and upload the app successfully. I saved the request from POSTMAN to its curl equivalent here it is, for the experts to quickly debug it.
--url 'http://127.0.0.1:10080/api/app/packagemanager/package?package=My%20HololensApp.appxbundle' \
--header 'Authorization: Basic c25hcmFpbjpzbmFyYWlu' \
--header 'Postman-Token: c6613653-3ff0-43c1-896c-63d62b125277' \
--header 'cache-control: no-cache' \
--header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
--form '=#C:\Users\snarain\Downloads\My HololensApp.appxbundle'```
The answer from jklemmack on this post helped solve this problem. Also I should be using ISO-8859-1 encoding instead of UTF-8 to make the appxbundle read successful.
powershell invoke-restmethod multipart/form-data
Solved !

API response is not showing in Powershell

I'm using Powershell v4.
I am calling a POST API and want to see the response when I run Invoke-RestMethod but it just shows a new line.
How can I output the response from the API to the console? I have tried running it in a script with Write-Host and Write-Output, but nothing appears on the console screen.
PowerShell script:
$Response = Invoke-RestMethod -Uri https://localhost:8081/myAPI -Body '{"username":"xyz","password":"xyz"}' -ContentType application/json -Method POST
Write-Host $Response
I can see the response when I curl the API but cannot see it in my PowerShell script which I need to use.
CURL
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' https://localhost:8081/myAPI
It looks like its not even hitting the API as nothing appears in the API logs when triggering from Powershell. Not sure how to resolve that if that is the problem.
Your command has a syntax error. Your contenttype is not in proper format.
You have:
-ContentType application/json
Change it to:
-ContentType "application/json"

Convert Gitlab CI Trigger Curl to Powershell Invoke-RestMethod

Does anyone know if it's possible to convert the curl command used to trigger builds in Gitlab-CI to a Powershell equivalent using Invoke-RestMethod?
Example curl command:
curl --request POST \
--form token=TOKEN \
--form ref=master \
--form "variables[UPLOAD_TO_S3]=true" \
https://gitlab.example.com/api/v3/projects/9/trigger/builds
This was taken from Gitlab's documentation page.
I found quite a few postings about converting a curl script for Powershell but I haven't had any luck in getting it to work. Here are some of the links I referenced:
How to send multipart/form-data with PowerShell Invoke-RestMethod
PowerShell equivalent of curl
Running curl via powershell - how to construct arguments?
Any help would be appreciated.
You can pass the token and the branch parameters directly in the URL. As for variables, putting it into the body variable should do the trick.
$Body = #{
"variables[UPLOAD_TO_S3]" = "true"
}
Invoke-RestMethod -Method Post -Uri "https://gitlab.example.com/api/v3/projects/9/trigger/builds?token=$Token&ref=$Ref" -Body $Body
Alternatively you can pass all arguments in the body parameter:
$form = #{token = $CI_JOB_TOKEN;ref = $BRANCH_TO_BUILD; "variables[SERVER_IMAGE_TAG]" = $CI_COMMIT_REF_NAME}
Invoke-WebRequest -Method POST -Body $form -Uri https://gitlab.example.com/api/v4/projects/602/trigger/pipeline -UseBasicParsing