How to Submit Timestamp Request to RFC 3161 Timestamp Server with Powershell - powershell

How would one submit a timestamp request to a RFC 3161 Timestamp Server using PowerShell's Invoke-WebRequest?
First, I create the timestamp request:
openssl ts -query -data message.txt -cert -sha256 -no_nonce -out ts_test_msg_sha256.tsq
Then I can use this curl command to submit it to the Timestamp server. This is confirmed to work.
curl -k -H "Content-Type: application/timestamp-query" -H "Host:timestamp.digicert.com" --data-binary #ts_test_msg_sha256.tsq http://timestamp.digicert.com > ts_test_msg_sha256.tsr
I am trying to do the same thing in PowerShell, but it isnt working. This is the PowerShell command I have tried:
Invoke-WebRequest -uri http://timestamp.digicert.com -Headers #{'Host' = 'timestamp.digicert.com'; 'Content-Type' = 'application/timestamp-query'} -body "ts_test_msg_sha256.tsq" -method POST > "ts_test_msg_sha256.tsr"
And I get this error:
Invoke-WebRequest : The remote server returned an error: (404) Not Found.
What am I doing wrong? As I understand it, the PowerShell command should be the exact same as the curl command.
EDIT: The final command I used was:
Invoke-WebRequest -uri http://timestamp.digicert.com -Headers #{'Host' = 'timestamp.digicert.com'; 'Content-Type' = 'application/timestamp-query'} -infile "ts_test_msg_sha256.tsq" -method POST -outfile ts_test_msg_sha256.tsr | Out-Null

The TS server returns 404 in response to incorrect requests.
The proper request is
$R = Invoke-WebRequest -uri http://timestamp.digicert.com/ -ContentType 'application/timestamp-query' -InFile "ts_test_msg_sha256.tsq" -method POST
Please note -ContentType and -InFile parameters.
The response is saved in $R variable. To write the raw response body to a binary file:
if ($R.StatusCode -eq 200) {
Set-Content ts_test_msg_sha256.tsr -Value $R.Content -AsByteStream;
echo "Done"
} else {
echo "Request failed: $($R.StatusCode)"
}

Related

Upload file to API endpoint with PowerShell using multipart/form-data

This curl command works:
curl -v -X POST https://subdomain.playvox.com/api/v1/files/upload?context=quality -H "Content-Type: multipart/form-data" -u [username]:[password] -F file=#c:\path\to\file.wav
But I am unable to perform the same thing in PowerShell using the Invoke-RestMethod cmdlet. Here's my attempt:
$file_contents = [System.IO.File]::ReadAllBytes($($file_path))
Invoke-RestMethod -Uri "https://subdomains.playvox.com/api/v1/files/upload?context=quality" -Method Post -ContentType "multipart/form-data" -Headers #{ "Authorization" = "Basic $($playvox_base64_auth)" } -Body #{ file = $file_contents }
When run the API responds with invalid_param, "file" is required. However I confirmed the call to ReadAllBytes succeeds and gives back the raw file data. It seems like PowerShell is not sending the request body in the right format? I've looked at several other answers here and documentation online and nothing I found has worked.
Discovered there is a -Form parameter in Powershell 6 and later. I updated my powershell and used the following instead:
$file_contents = Get-Item $file_path
Invoke-RestMethod -Uri "https://subdomains.playvox.com/api/v1/files/upload?context=quality" -Method Post -ContentType "multipart/form-data" -Headers #{ "Authorization" = "Basic $($playvox_base64_auth)" } -Form #{ file = $file_contents }
And it worked.

Converting Curl API Call to Invoke-WebRequest in PowerShell 5

I have done a bunch of searches, but nothing so far as helped me solve this. Any help is appreciated.
I am trying to convert a curl command to Invoke-WebRequest, but for some reason I am not getting a response back. I have tried:
$headers = #{
outh_a = "WEBREQ";
oauth_key = "key";
etc...
}
Invoke-WebRequest -Uri $url -Method POST -Body "{}" -Headers $headers -Verbose
The curl command looks like this:
.\curl.exe -x POST -d "{}" -H "oauth_a: WEBREQ" -H "oauth_key:key" -H "Content-Type: application/json" -H "oauth_sm:HMAC-SHA2" -H "oauth_t:timestamp"-H "oauth_s:key+signature=" "https://example.com/services/api/request?t=data&a=data2&n=404&i=0"
The command works perfectly and returns the data. I am using PowerShell as we want to parse through the data once received. Any help is greatly appreciated.
The curl command will fail if I submit it without -d "{}", so that part is required. The server I guess is expecting to receive a specific amount of data.
I am not sure what is going on to prevent a response. I have tried curl from the same machine I am doing the PowerShell script on and it works. I even used SoapUI to make the same call and it works there too.
Edit:
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.WebRequest]::DefaultWebProxy.Credentials =
[System.Net.CredentialCache]::DefaultCredentials
[Net.ServicePointManager]::SecurityProtocol =
[Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls,
[Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12
$Domain = "https://example.com/services/api/request?t=oauth_auth_token&a=appid&n=404&i=0"
# OAUTH Authorization Request Header
$Header = #{
o_nonce = 'TEST'
o_con_key = 'key'
o_sig_meth = 'type'
o_timestamp = 'timestamp'
o_sig = 'signature'
o_ver = '1.0'
o_realm = 'http://example.com/services'
}
$Body = #{}
$API_Response = Invoke-RestMethod -Method POST -Headers $Header -Uri $Domain -Body $Body -Verbose -TimeoutSec 20
Update:
#MikeW I'd have to look at your PS code in more detail, could you post the entire API call minus the sensitive information (api key etc...)?
Also, on a side note, sometimes just passing the parameters on the URI works just as well. Have you tried passing the parameters through the URI? You can see the following example of an API call I'm making using just the URI:
Invoke-RestMethod -Uri ("$EndpointURL/Tickets/$TicketID/?username=$ServiceAccount&apiKey=$APIKey");

convert curl command to invoke-WebRequest or Invoke-RestMethod

I have a requirement to convert a working curl command to Invoke-WebRequest command
The command is used to create a project in SonarQube
Curl:
curl -u as123123112312: -X POST "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%" > NUL
the command I tried:
$e = #{
Uri = "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%"
Headers = #{"Authorization" = "Token as123123112312"}
}
Invoke-WebRequest #e -Method POST
Error:
Invoke-WebRequest : The remote server returned an error: (401) Unauthorized
Does anyone have an idea of converting curl to invoke-webrequest
This has been asked before. When you are posting, you need to have a body to send too, example:
$username = "as123123112312";
$password = "blah";
$Bytes = [System.Text.Encoding]::UTF8.GetBytes("$username`:$password");
$encodedCredentials = [System.Convert]::ToBase64String($bytes);
$body = "your content (i.e. json here)";
Invoke-WebRequest -Method Post -Body $body -Headers #{ "Authorization" = "Basic " + $encodedCredentials} -Uri "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%"

Invoke-RestMethod uploading CSV in ServiceNow

I tried many ways of uploading a CSV file to ServiceNow using PowerShell via Invoke-RestMethod and Invoke-WebRequest; however, I have hit a wall. When I call the functions, I receive the following error:
"Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed."
"Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed."
I have tried the code below:
Attempt 1)
$Headers = #{'Auth_token'=$envCred};
$FileContent = [IO.File]::ReadAllText('C:\temp\test.csv');
$Fields = #{'uploadFile'=$FileContent};
Invoke-WebRequest -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Headers $Headers -Body $Fields;
Attempt 2)
Invoke-RestMethod -Method Post -Uri $uri -Credential $envCred -ContentType 'multipart/form-data -InFile "C:\temp\test.csv" '
I know for a fact that the API is working, because after I call:
Invoke-RestMethod -Method 'get' -Uri $uri -Credential $snCred -body $body it returns the proper information.
I also tried the [-Method Patch] with the following: "Invoke-RestMethod -Uri $uri -Credential $snCred -Method Patch -ContentType 'text/csv' -InFile "C:\temp\test.csv" - Also tried with the -ContentType 'multipart/form-data' - I get the following error: "Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type."
Is there another way or uploading CSV file(s) in PowerShell using the Invoke-RestMethod or Invoke-WebRequest?
Solution:
I realized that I had a typo with my URI, after fixing it, it worked! Sorry for the inconvenience.
It's unclear what endpoint you were using, however, here's an example from the docs
curl "https://instance.service-now.com/api/now/attachment/upload" \
--request POST \
--header "Accept:application/json"
--user 'admin':'admin'"\
--header "Content-Type:multipart/form-data"
-F 'table_name=incident' -F 'table_sys_id=d71f7935c0a8016700802b64c67c11c6' -F 'encryption_context=undefined'-F 'uploadFile=# location of the file on file system'

how to convert curl command in Invoke-RestMethod of powershell

I need to convert the following command from bash curl to powershell syntax:
curl -s --head --header "PRIVATE-TOKEN:XXXXXX" "https://gitlab.XXXXXX/api/v4/projects/${id}/issues?state=all&per_pages=100"
This one in particular to get value of X-Total-Page from results.
I've tried to convert with this, but doesn't works:
function getPageNumbers ($myId)
{
$privateToken = "myToken"
$headers = #{"PRIVATE-TOKEN"="$privateToken"}
$url = "https://gitlab.XXXXXX/api/v4/projects/$myId/issues?state=all&per_pages=100"
$result = Invoke-RestMethod -Method Head -Uri "$url" -Header $headers -ContentType "text/json"
return $result
}
maybe because the "Head" method is used only for Invoke-Webrequest ?
Any suggestions?
Thank you in advance!
I am not seeing the body. But you can add that. You can do something like this:
$privateToken = "myToken"
$headers = #{"PRIVATE-TOKEN"="$privateToken"}
Invoke-WebRequest -Uri "https://gitlab.XXXXXX/api/v4/projects/$myId/issues?state=all&per_pages=100" -ContentType "application/json" -Headers $headers -Method Post
I would also suggest you to go through the CURL to Invoke-WebRequest
Hope it helps.