Error cURL AbuseIPDB through Powershell Command - Cannot bind parameter 'Headers' - powershell

I'd like to ask about cURL command for AbuseIPDB, as stated in https://docs.abuseipdb.com/#check-endpoint
So I tried to run the command with the following Powershell curl command"
curl -G https://api.abuseipdb.com/api/v2/check \ --data-urlencode "ipAddress=118.25.6.39" \ -d maxAgeInDays=90 \ -H "Key: $MYKEY" \ -H "Accept: application/json"
However, I received the following error description:
Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Key: " value of type "System.String" to type "System.Collections.IDictionary".
At line:1 char:120
+ ... ys=90 \ -H "Key: $MYKEY ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I wonder what I did wrong in here? Thank you very much for the pointers/ answers.

The headers argument is supposed to receive a dictionary (once), not a string (multiple times), e.g.
curl ... -H #{ "Key" = "$MYKEY"; "Accept" = "application/json" }

Related

Problem with cURL to Powershell conversion

I have been trying to convert the below cURL command to PowerShell so that I can use it on my script. I converted it but, every time I execute the command, it gives me error like in the screenshot.
cURL command: curl -X POST "https://www.hybrid-analysis.com/api/v2/quick-scan/file?_timestamp=1585901455112" -H "accept: application/json" -H "user-agent: API_KEY" -H "Content-Type: multipart/form-data" -F "scan_type=all" -F "file=#xyz.png;type=image/png" -F "no_share_third_party=false" -F "allow_community_access=false"
converted powershell command:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
$fileqq = Get-ChildItem -Path "C:\temp\xyz.jpg"
Invoke-WebRequest "https://www.hybrid-analysis.com/api/v2/quick-scan/file" -Headers #{'accept' = 'application/json'; 'user-agent' = 'Falcon Sandbox' ; 'Content-Type' = 'multipart/form-data' ; 'api-key' = "API_KEY"} -Method Post -Body #{"scan_type" = "all"; "file" = "$fileqq" }
powershell command error
It would be helpful for me if someone pointed out the error or correct my converted command
Thank you very much for the help.

cURL command works in git bash but not in cmd and powershell

The folowing command works in git bash but not in cmd and powershell
curl -X POST http://localhost:5678/api/findgen -H 'Content-Type: application/json' -d '{"a": "Val 1","b": "Val 2","c": "Val 3","d": "Val 4"}' -o "file.json"
I get error in cmd such as -
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: Val 1,b
curl: (6) Could not resolve host: Val 2,c
curl: (6) Could not resolve host: Val 3,d
curl: (3) [globbing] unmatched close brace/bracket in column 6
What can be the issue?
Just read the error message:
Invoke-WebRequest Cannot bind parameter 'Headers'. Cannot convert the "Content-Type:
application/json" value of type "System.String" to type
"System.Collections.IDictionary".
In PowerShell, curl is an alias for the Invoke-WebRequest cmdlet. As the error points it out, the Header parameter must be a IDictionary, not a string. This is how it looks like in PowerShell:
#{"Content-Type"= "application/json"}
Some parameters are also different. This is how I would script the request:
Invoke-WebRequest `
-Uri "http://localhost:5678/api/findgen" `
-Headers #{"Content-Type"= "application/json"} `
-Body '{"a": "Val 1","b": "Val 2","c": "Val 3","d": "Val 4"}' `
-OutFile "file.json" `
-Method Post
So here is the adapted syntax as a one liner :
curl.exe "-X" "POST" "https://reqbin.com/echo/post/json" `
"-H" "Content-Type: application/json" `
"-d" '{\"a\": \"Val 1\",\"b\": \"Val 2\",\"c\": \"Val 3\",\"d\": \"Val 4\"}' `
"-o" "file.json"
And a way a little more friendly for JSON :
curl.exe "-X" "POST" "https://reqbin.com/echo/post/json" `
"-H" "Content-Type: application/json" `
"-d" (#{a='Val 1';b='val 2';c='Val 3';d='Val 4'} | ConvertTo-Json -Compress) `
"-o" "file.json"
I see those "Could not resolve host" only with C:\Windows\System32\curl.exe on Windows 10 (build 17063 or later), because of the way the Windows curl.exe native program interprets single and double quotes.
If you inverse/escape those quotes, it does work (on CMD)
C:\Windows\System32\curl -X POST http://localhost:5678/api/findgen \
-H "Content-Type: application/json" \
^^^
double-quotes
-d "{\"a\": \"Val 1\",\"b\": \"Val 2\",\"c\": \"Val 3\",\"d\": \"Val 4\"}" -o "file.json"
^^^
double quotes outside, escaped double-quotes inside: valid JSON
This is similar to "How do I POST JSON with Curl?"
On Powershell, I had to escape double quotes (backtick, as commented), and escape {}: {{}}
PS D:\> C:\Windows\System32\curl -X POST https://reqbin.com/echo/post/json `
-H "Content-Type: application/json" `
-d "{{`"login`":`"my_login`",`"password`":`"my_password`"}}"
Alternatively:
PS D:\> C:\Windows\System32\curl.exe -X POST https://reqbin.com/echo/post/json `
>> -H "Content-Type: application/json" `
>> -d '{{""login"":""my_login"",""password"":""my_password""}}'
{"success":"true"}
From Steven's post:
PS D:\> C:\Windows\System32\curl.exe -X POST -H "Content-Type: application/json" `
-d '{""north"": ""each south""}' https://reqbin.com/echo/post/json
{"success":"true"}
Using curl.exe alone means using the MingW Git for Windows curl.exe
PS D:\> curl.exe -X POST -H "Content-Type: application/json" `
>> -d '{""north"": ""each south""}' https://reqbin.com/echo/post/json
curl: (6) Could not resolve host: each
curl: (3) unmatched close brace/bracket in URL position 6:
south} https://reqbin.com/echo/post/json
You therefore need to escape curly brackets, and remove any space:
PS D:\> curl.exe -X POST -H "Content-Type: application/json" `
>> -d '{{"north":"each south"}}' https://reqbin.com/echo/post/json
{"success":"true"}
Or, using spaces, with single quotes:
PS D:\> curl.exe -X POST -H "Content-Type: application/json" `
>> -d '{\"north\": \"each south\"}' https://reqbin.com/echo/post/json
{"success":"true"}
On CMD If you want to keep those quotes unchanged, used the mingw curl.exe packaged with Git For Windows, at least on CMD.
C:\path\to\git\mingw64\bin\curl.exe -X POST http://localhost:5678/api/findgen \
-H 'Content-Type: application/json' \
-d '{"a": "Val 1","b": "Val 2","c": "Val 3","d": "Val 4"}' -o "file.json"
^^^
You can keep the quotes as-is
As commented, in a Powershell session, curl itself is an alias for Invoke-WebRequest.
I tested it with:
PS D:\> Get-Alias -Name curl
CommandType Name Version Source
----------- ---- ------- ------
Alias curl -> Invoke-WebRequest
PS D:\> curl -uri https://reqbin.com/echo/post/json `
>> -Method 'POST' -ContentType "application/json" `
>> -Body "{""login"":""my_login"",""password"":""my_password""}"
StatusCode : 200
StatusDescription : OK
Content : {"success":"true"}
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
CF-Cache-Status: DYNAMIC
cf-request-id: 0917a97460000053c82aa69000000001
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/bea...
Forms : {}
Headers : {[Connection, keep-alive], [CF-Cache-Status, DYNAMIC], [cf-request-id, 0917a97460000053c82aa69000000001], [Expect-CT, max-age=604800,
report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : System.__ComObject
RawContentLength : 19

cURL to PowerShell - Double hash table in --data?

I've been trying (and failing for hours) to convert this cURL script into PowerShell:
curl -X POST \
https://example.net \
-H 'Authorization: Bearer 1234567890' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"data": {
"MID": 33,
"DID": "66666666",
"CID": 10002,
"status": "ACTIVE",
"HID": "11"
}
}'
My PowerShell script looks like this - I suspect it's something to do with the double hash table I've created in $Body but I am really at a loss.:
.. script snipped for simplicity
$Body = #{
'data'= #{
'MID'= 33;
'DID'= "66666666";
'CID'=10002;
'status'="ACTIVE";
'HID'= "11"
}
}
$CurlArgument = '-X', 'POST',
'https://example.net',
'-H', 'Authorization: Bearer 1234567890',
'-H', 'Content-Type: application/json',
'-d',
$Body
$CURLEXE = 'C:\Windows\System32\curl.exe'
& $CURLEXE #CurlArgument
I get the following error message when it executes:
..."message":"Access not allowed","details":{"5011":"A JSONObject text must begin with '{' at 1 [character 2 line 1]"}}]}
I experimented with adding after $Body:
| ConvertTo-Json
but that then gives me this error:
Unexpected character ('d' (code 100)): was expecting double-quote to start field name
My $CurlArgument variable looks like this (which looks the same as the first cURL script):
-X
POST
https://example.net
-H
Authorization: Bearer 1234567890
-H
Content-Type: application/json
-d
{
"data": {
"CID": 10002,
"MID": 33,
"HID": "11",
"DID": "66666666",
"status": "ACTIVE"
}
}
As always, assistance would be greatly appreciated.
Your $Body variable contains a hashtable (#{ ... }), so you must explicitly convert it to JSON with ConvertTo-Json.
Additionally, because you're calling an external program, you must escape the " chars. in the JSON string as \"[1]:
$CurlArgument = '-X', 'POST',
'https://example.net',
'-H', 'Authorization: Bearer 1234567890',
'-H', 'Content-Type: application/json',
'-d',
(($Body | ConvertTo-Json) -replace '"', '\"')
[1] This additional escaping requirement is highly unfortunate, but to date it has been kept around for the sake of backward compatibility.
This GitHub docs issue tells the whole story.

cUrl vs Invoke-WebRequest

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.

Problems bulk insert elasticsearch curl powershell

I'm new to Elasticsearch and I'm following the tutorial loading sample data: https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html
When I try to bulk insert the data in powershell
'curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/shakespeare/_bulk?pretty' --data-binary "#shakespeare.json"'
I get the following error
PS C:\Development\elasticsearch-5.4.1\importdata> curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/shakespeare/_bulk?pretty' --data-binary "#shakespeare.json"
Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/x-ndjson" value of type "System.St
ring" to type "System.Collections.IDictionary".
At line:1 char:9
+ curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I've tried several things, read the bulk api documentation but with no result
Here're some examples of the content of the json file:
{"index":{"_index":"shakespeare","_type":"act","_id":0}}
{"line_id":1,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"}
{"index":{"_index":"shakespeare","_type":"scene","_id":1}}
{"line_id":2,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"SCENE I. London. The palace."}
{"index":{"_index":"shakespeare","_type":"line","_id":2}}
{"line_id":3,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others"}
I found the solution. Because I used curl on windows and with powershell, I had to translate
curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/shakespeare/_bulk?pretty' --data-binary #shakespeare.json
to
PS C:\Development\elasticsearch-5.4.1\importdata> $postParams = #"
>> {"index":{"_index":"shakespeare","_type":"act","_id":0}}
>> {"line_id":1,"play_name":"Henry IV","speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"}
>>
>> "#
PS C:\Development\elasticsearch-5.4.1\importdata> curl -H #{"Content-Type" = "application/x-ndjson"} -Method POST 'http://localhost:92
00/shakespeare/_bulk?pretty' -body $postParams
When I use the demo file, the bulk insert didn't succeeded but that's another problem
PS C:\Development\elasticsearch-5.4.1\importdata> $postParams = Get-Content "shakespeare.json"