API response is not showing in Powershell - rest

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"

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.

Powershell equivalent to curl -F to upload a file

I need to upload files via commandline / powershell.
It works fine with
curl -F "file=#test.txt" https://api.anonfiles.com/upload
However, curl does not exist on Windows Server 2016 and I do not want to tell my clients to set it up. So I am looking for a powershell alternative to accomplish this task. I tried various solutions, but none of them worked. What I tried so far:
(1)
$postParams = #{file='C:\users\user\testfile.txt'}; Invoke-WebRequest -Uri https://api.anonfiles.com/upload -Method POST -Body $postParams
(2)
Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -InFile "C:\users\user\testfile.txt"
(3)
$file=[io.file]::readallbytes('c:\users\user\testfile.txt')
Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -Body "{ '#file':'"$file"'}"
None of it works. I canot believe it's so hard to replace an curl oneliner in powershell... The error in each case is a 400 http error, the request is wrong.
How do I send the above mentioned curl-request equivalent in powershell? The site is https://anonfiles.com/docs/api
I gave up. It's not possible in Powershell.
My App will just download curl.exe as standalone from now on, if it's not there.

Powershell Post API Request

I'm able to post the request using bash with the following:
#!/bin/bash
APIKey="apikeyhere"
content="{\"accessToken\":\"$APIKey\",\"elements\":[{\"serialnumber\":\"AAAAAAAAA\",\"name\":\"EXAMPLENAME\",\"tags\":\"EXAMPLETAG\"}]}"
curl -s -k -X POST -d 'content='$content 'https://apiaccess.example.com/v2/devices'
I tried to use powershell but get an error "INVALID REQUEST":
$body = #{
"accessToken"="APIKeyhere"
"elements" = #{
"serialnumber"="AAAAA"
"name"="DeviceName"
"tags"="tag1,tag2"
}} | ConvertTo-Json
$header = #{
"Accept"="application/json"
"Content-Type"="application/json"
}
Invoke-RestMethod -Uri "https://apiaccess.example.com/v2/devices" -Method 'Post' -Body $body -Headers $header | ConvertTo-HTML
Any pointed regarding how I can fix the powershell script?
Don't do this: | ConvertTo-Json
Your headers can be replaced with this: -ContentType application/json
You should probably not convert the results to HTML until you've experimented with the data you get back. But that's up to you.
I did something similar once and had to translate a CURL request into Powershell. Maybe the following article will help you:
CURL to Powershell example
i got also the Error
"INVALID REQUEST":...
In my case, the API was weird. CURL made a simple fallback of a GET request to the POST method... it took hours to realize that I had to do a POST instead of a GET in Powershell.

Invalid Body using Invoke-Webrequest

I'm trying to post to a Wazuh server using the following guidance:
https://documentation.wazuh.com/3.x/user-manual/api/reference.html#add-agent
This is the CURL equivalent:
curl -u foo:bar -k -X POST -d '{"name":"NewHost","ip":"10.0.0.9"}' -H 'Content-Type:application/json' "https://127.0.0.1:55000/agents?pretty"
This is what I've come up using Powershell:
Invoke-WebRequest -Body "{name:'newhost',ip:'10.0.0.9'}" -Uri "http://$($WAZUHSERVER):55000/agents?pretty" -Method Post -Credential $MyCredential -ContentType 'application/json'
I keep getting "Invalid Request Body".
I tried alternating with a hash table. No dice. I should mention that I dont have SSL configured so that part is irrelevant.
Found it.
'{"name":"newhost","ip":"10.0.0.0"}'
Using this one as a body worked out perfectly.

Powershell Multi-part-form-data convert from curl

I'm trying to run a powershell script to perform an invoke-webrequest using the -ContentType multi-part/form-data parameter.
I have an example of a working curl command and need to convert it to powershell.
curl -v -u user#yourcompany.com:test -F "helpdesk_ticket[attachments][][resource]=#/path/to/attachment1.ext" -F "helpdesk_ticket[attachments][][resource]=#/path/to/attachment2.ext" -F "helpdesk_ticket[email]=example#example.com" -F "helpdesk_ticket[subject]=Ticket Title" -F "helpdesk_ticket[description]=this is a sample ticket" -X POST https://domain.webaddress.com/helpdesk/tickets.json
I have tried to do this with hash tables but having no luck.
So far I have:
$body = #{"helpdesk_ticket[attachments][][resource]"="$/path/to/attachment.ext";"helpdesk_ticket[email]"="example#example.com"; "helpdesk_ticket[subject]"="Ticket Title"; "helpdesk_ticket[description]"="this is a sample ticket"}
Invoke-WebRequest -Uri https://domain.webaddress.com/helpdesk/tickets.json -Headers $FSHeaders -ContentType multi-part/form-data -Body $Body -Method POST
The error returned suggests the data in the body is incorrect as authentication (hidden in the header variable) is correct.