PS C:\> $postParams = #{eventId='235'}
PS C:\> curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events -Body $postParams
curl : Error deleting event
At line:1 char:1
+ curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events -Body $po ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], Web
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
However, if I am trying to delete like
curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events?eventId=235
it works
Why is not working in the first way using $postParams ?
This is not working
PS C:\Users\> $postParams = "{eventId='$eventId'}"
PS C:\Users\> Invoke-WebRequest -Method POST -Uri "http://localhost:8080/eventlist/api/v1/events" -Body $postParams
Invoke-WebRequest : Error creating event
At line:1 char:1
+ Invoke-WebRequest -Method POST -Uri "http://localhost:8080/eventlist/api/v1/even ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
This is working
PS C:\> Invoke-WebRequest -Method DELETE -Uri 'http://localhost:8080/eventlist/api/v1/events?eventId=235'
StatusCode : 200
StatusDescription : OK
Content : Event deleted successfully
RawContent : HTTP/1.1 200 OK
Content-Length: 26
Content-Type: text/plain;charset=ISO-8859-1
Date: Mon, 20 Feb 2017 12:27:46 GMT
Server: Apache-Coyote/1.1
Event deleted successfully
Forms : {}
Headers : {[Content-Length, 26], [Content-Type, text/plain;charset=ISO-8859-1], [Date, Mon, 20 Feb 2017
12:27:46 GMT], [Server, Apache-Coyote/1.1]}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 26
EDIT
It's failing because DELETE isn't a POST command.
The code below is untested.
To recreate the DELETE in PowerShell, your syntax needs to be:
$eventId=235
Invoke-WebRequest -Method DELETE -Uri "http://localhost:8080/eventlist/api/v1/events?eventId=$eventId"
ORIGINAL POST
This relates to the commandline app curl, not the PowerShell curl which is an alis for Invoke-WebRequest
It's failing for two reasons, The first one is that DELETE isn't a POST command. The second, is that you're trying to pass a PowerShell object into a commandline application.
The code below is untested.
To recreate the DELETE in PowerShell, your syntax needs to be:
$eventId=235
&curl -Method DELETE -Uri "http://localhost:8080/eventlist/api/v1/events?eventId=$eventId"
A POST command could be like this (depending on your endpoint):
$eventId=235
$postParams = "{eventId='$eventId'}"
&curl -H "Content-Type: application/json" -X POST -d $postParams 'http://localhost:8080/eventlist/api/v1/events'
Note, the body is a json string, not a PowerShell object.
Related
I am creating a script to call a couple of endpoints in my service.
The calls look like this:
$scheduleResponse=(curl -Method 'POST' -Uri $baseUrl'/scheduletests' `
-Headers #{"Accept"="application/json";"Content-Type"="application/json"} `
-Body $body)
$scheduleContent=ConvertFrom-Json([String]::new($scheduleResponse.Content))
#Error checks here
$testId = [String]::new($scheduleContent.ScheduleTests.TestID)
$getTestResultsBody = '{"TestID":"' + $testId + '"}' #Hard-coding this value works.
$getResponse=(curl -Method 'POST' -Uri $baseUrl'/gettestresults' -Headers #{"Accept"="application/json";"Content-Type"="application/json"} -Body $getTestResultsBody)
My getResponse fails with this error message:
curl : Unable to read data from the transport connection: The
connection was closed. At C:\Development\Services\TESTService\Test
Clients\Caller.ps1:43 char:27 ... etResponse=(curl -Method 'POST'
-Uri $baseUrl'/gettestresults' -Heade ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], IOException
FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I debugged my service and it is receiving the correct request and sending the correct response back.
If I hard-code testId in the getRequest, the whole script works. So my guess is that it has something to do with reading the test id from the first call.
Any pointers on what could be wrong?
So, here is the link to Jobs API call in Databricks here
Everything works in Python using requests. E.g both the job creation and job listing works
import requests
proxy= "http://127.0.0.1:8888"
access_token="tokenabc"
proxies = {
"https": proxy,
}
header_read = {
'Authorization': "Bearer " + access_token,
'Accept': "application/scim+json"
}
#list jobs
init_get=requests.get('https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/list', headers=header_read, proxies=proxies)
#create job
init_post=requests.post('https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/create', headers=header_read, proxies=proxies,verify=False,json=job_payload)
However, strangely in Powershell, only the job creation works and Job listing fails.
#this works
Invoke-RestMethod -Uri 'https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/create' -Headers #{ 'Authorization' = "Bearer $bearertoken" } -Method Post -Body $content -ContentType 'application/json'
#this does not work
Invoke-WebRequest -Uri 'https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/list' -Headers #{ 'Authorization' = "Bearer $bearertoken" } -Method Get
#RestMethod also does not work
Invoke-RestMethod -Uri 'https://databricksworkspaceid.azuredatabricks.net/api/2.0/jobs/list' -Headers #{ 'Authorization' = "Bearer $bearertoken" } -Method Get
I have also tried setting the Content type on these but nothing helps.
Also, explicitly setting the proxy (fiddler) does not help.
-proxy "http://127.0.0.1:8888"
But should not be the proxy also , as the post method works.
I just keep getting an error like
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:22 char:5
+ Invoke-WebRequest -Uri 'https://databricksworkspaceid.azuredatabricks.net/ap ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
or in case of RestMethod
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:22 char:5
+ Invoke-RestMethod -Uri 'https://databricksworkspaceid.azuredatabricks.net/ap ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I could have understood if everything failed in Powershell, but the post method (job creation) works, so not sure, why the connection would be terminated in case of a Get request but not a post.
Going by some forum posts, I have also tried the following but to no avail-
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Does anyone know what am I doing wrong/missing? Baffling to see working in python but only a part in Powershell.
So, I finally found the answer. Basically 2 things:
Had to do this before hitting the list endpoint (strangely as I said- the create endpoint) worked
Basically allowing TLS, TLS1.1 and TLS1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
Doing only point 1 didn't work. I HAD to also use an "elevated" powershell session. Basically running the script with step 1 included, using a "Run as Administrator".
Invoke-RestMethod : The underlying connection was closed: An unexpected error
occurred on a send.## Heading ##
At \Desktop\T.ps1:27 char:7
+ $CS = Invoke-RestMethod -Method PUT -Uri https://XXXX ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt
pWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
ll.Commands.InvokeRestMethodCommand
Your error is in the script T.ps1 line: 27 char: 7, and it is related to the Invoke-ResMethod cmdlet. I'll give you an example on how to use Invoke-RestMethod.
Invoke-RestMethod -Uri "place the dns or ip you want to request" -Method (POST or Get)
Working example to request google:
Invoke-RestMethod -Uri "https://www.google.com" -Method GET
This must return the html code of google's web page
I'm trying to convert this working request done in Cygwin to Powershell:
Cygwin (Working):
curl -k -u curl:Password -X PUT -F "file=$($_)" https://$($appliance)/wse/customblockpage/file/en
Powershell (not working):
Invoke-Webrequest -Uri "https://$($appliance)/wse/customblockpage/file/en" -Method Put -Infile "$homePath\$($_)" -Credential $cred
Here is the error I get:
Invoke-Webrequest : { "Error": "No file part in file", "Result": "Failure" }
At line:1 char:1
+ Invoke-Webrequest https://{IP Address Masked}/wse/customblockpage/file/en ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
The { "Error": "No file part in file", "Result": "Failure" } is actually the error response from the web server and not a specific PowerShell error message.
In your cURL invocation, you are specifying form data with the -F flag, yet you don't appear to be doing the same in PowerShell.
In PowerShell, you can specify form data using the -Body flag like this:
Invoke-Webrequest -Uri "https://example.com/" -Method Put -Body #{ "file" = "hello.txt" }
If you need to send the actual content of the file, then you can use this as your -Body argument:
-Body #{ "file" = (Get-Content hello.txt) }
Can anybody explain to me why cUrl (the real cUrl) works but Invoke-WebRequest doesn’t? Same machine, same variables. To me it looks like they should both be doing the same thing, uploading a file to jfrog Artifactory.
$headers = #{
'X-JFrog-Art-Api' = $apiKey
"Content-Type" = "application/json"
"Accept" = "application/json"
}
Invoke-WebRequest -InFile $file -Method Put -Uri "$ARTIFACTORY_HOST/third-party/test/readme.md" -Headers $headers -Verbose
This PowerShell doesn't work.
curl -T readme.md "${ARTIFACTORY_HOST}/third-party/test/readme.md " \
-H "X-JFrog-Art-Api: ${apiKey}" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
cUrl works.
PowerShell fails with
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest -InFile $file -Method Put -Uri "https:// ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Turns out PowerShell defaults to the wrong TLS version and needs to be specifically told to use 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Put that in front of the Invoke-WebRequest and all is fine.