Unable to use curl with double quote using powershell (Hubspot API) - powershell

I use Hubspot API.
I try to craete contact info.
Type this code using powershell (5.1version and 7.2)
curl.exe --request POST --url "https://api.hubapi.com/crm/v3/objects/contacts?hapikey=$apiKey"
--header 'content-type: application/json' -d "{\"properties\": {\"email\":\"$email\",\"firstname\":\"$firstname\"}}"
but error
return {"status":"error","message":"Invalid input JSON on line 1, column 3: Unexpected character ('\\' (code 92)): was expecting double-quote to start field name","correlationId":"2086b3bc-abe5-4f90-a79c-213d45bb2a97"}
I try this code using single quote after -d.
and then create contact info.
curl.exe --request POST --url "https://api.hubapi.com/crm/v3/objects/contacts?hapikey=$apiKey" --header 'content-type: application/json' -d '{\"properties\": {\"email\":\"xxx#yyy.co.jp\",\"firstname\":\"k\"}}'
but I want to substitute variables. So I want to use double quote.

I solved by myself.
put double-quotes and backslash between variable.
ConvertTo-Json
setting 2. to curl.exe
like this
$contactBody = #{
'"properties"' = #{
'"firstname"' = "`"$firstname`""
'"email"' = "`"$email`""
}
}
$contactBodyJson = ConvertTo-Json $contactBody
curl.exe --request POST --url "https://api.hubapi.com/crm/v3/objects/contacts?hapikey=$apiKey" --header 'content-type: application/json' -d $contactBodyJson

Related

Convert Powershell command to curl

I am trying make the following API calls with a curl command and run it on Linux:
https://octopus.com/blog/manually-push-build-information-to-octopus
This what I got:
curl -X POST https://YourServerUrl -H "X-Octopus-ApiKey"="API-XXXXXXXXXXXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" -d "#jsonBody"
I am not sure how to convert this script to a json in curl
$jsonBody = #{
PackageId = "twerthi/xCertificatePermission"
Version = "1.0.0"
OctopusBuildInformation =
#{
BuildEnvironment = "Jenkins"
VcsCommitNumber = "2350881a389517288b31432d469c5c4199a1fba9"
VcsType = "Git"
VcsRoot = "https://github.com/twerthi/xCertificatePermission.git"
}
} | ConvertTo-Json -Depth 10
The curl command -d (--data) is the specified data in the POST request. So you should be able to just enter valid JSON data as part of the call. i.e. something like this:
curl -X POST https://YourServerUrl -H "X-Octopus-ApiKey"="API-XXX" -H "Content-Type: application/json" -d '{ "PackageId":"twerthi/xCertificatePermission", "Version":"1.0.0", "OctopusBuildInformation":{ "BuildEnvironment":"Jenkins", "VcsCommitNumber":"2350881a389517288b31432d469c5c4199a1fba9", "VcsType":"Git", "VcsRoot":"https://github.com/twerthi/xCertificatePermission.git"}}'
Note, if you are testing this in cmd/bash etc, you can split the command over multiple lines by using an escape character. Windows: ^ Linux/MacOS: \
Example in Windows:
curl -X POST https://YourServerUrl ^
-H "X-Octopus-ApiKey"="API-XXX" ^
etc....
Also, assuming that's valid PS, you can just run it and check the result in $jsonBody to see how its formatted.
Assuming you want to call the external curl utility from PowerShell (note that on Windows, in Windows PowerShell, you'll have to call it as curl.exe):
curl -X POST https://YourServerUrl `
-H 'X-Octopus-ApiKey: API-XXXXXXXXXXXXXXXXXXXXXXXXXX' `
-H 'Content-Type: application/json' `
-d ($jsonBody -replace '([\\]*)"', '$1$1\"')
Note the unfortunate need for a -replace operation on the $jsonBody variable containing your JSON string, which - as of PowerShell 7.1 - is needed to work around a long-standing bug, discussed in this answer.

translate Postman post Request with Basic Auth to mingw64 curl and powershell On Windows 10

I would like to translate a POST Request test from Postman to curl on Windows!
note: ip, username, password are illustrative for this question!
Consuming from Postman, I get 200 OK Code fine!
But, Trying to use curl from cmd is not working!
Checking on Postman the curl code
I was trying on Powershell
curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --header 'Authorization: Basic bWFyaWVsYTpZMFMkaSRfViZTLjQwNio=' --header 'Content-Type: application/json' --data-raw '{"ANI" : 3007209820}'
curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --header 'Authorization: Basic bWFyaWVsYTpZMFMkaSRfViZTLjQwNio=' --header 'Content-Type: application/json' --data-raw '{\"ANI\" : 3007209820}'
checking codification/decodification is fine.
Now directly with user and password, according to https://stackoverflow.com/a/27442239/811293
curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --user 'mariela:Y0S$i$_V&S.406*' --header 'Content-Type: application/json' --data-raw '{"ANI" : 3007209820}'
Here my results.
When I use curl of mingw64 7.71.1 version, the command is misinterpreted, because I get a 407 error, Authentication required. It's not just a powershell problem solved with Remove-item alias:curl.
I suspect you should escape some characters, and use double commas " instead of single commas '.
What considerations should I have with $, _, &, . and * characters (if I should have it with any)?
How should I write the request in powershell (or mingw64 curl) for it to work?
The generated command is not powershell curl but cmd curl :
you can just use it in cmd as it is,
or if you want to use powershel use it as:
$command= "curl --location --request POST 'http://10.31.7.52/BdN_SMS_CXP/api/SendSMS' --header 'Authorization: Basic bWFyaWVsYTpZMFMkaSRfViZTLjQwNio=' --header 'Content-Type: application/json' --data-raw '{""ANI"" : 3007209820}'"
cmd /c $command

Azure DevOps problem to create project via REST call containing german umlauts

I have an admin script that creates Azure DevOps projects via calls to the REST API. This works fine as long as the name or description arguments contain no special characters. If for example german umlauts are in the description, the call fails.
This works:
curl -k -D- -X POST -d '{"name":"Phantom","capabilities":{"processTemplate":{"templateTypeId":"6b724908-ef14-45cf-84f8-768b5384da45"},"versioncontrol":{"sourceControlType":"Git"}},"visibility":"private","description":"Innocent description without umlauts"}' -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Basic secretauthorizationtoken=' https://dev.azure.com/AdminPlayground/_apis/projects?api-version=6.0-preview.4
this one throws an exception:
curl -k -D- -X POST -d '{"name":"Phantom","capabilities":{"processTemplate":{"templateTypeId":"6b724908-ef14-45cf-84f8-768b5384da45"},"versioncontrol":{"sourceControlType":"Git"}},"visibility":"private","description":"Funny description with strange äöüß chars"}' -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Basic secretauthorizationtoken=' https://dev.azure.com/AdminPlayground/_apis/projects?api-version=6.0-preview.4
The exception is:
{"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: projectToCreate","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
Has anybody experienced similar behavior, and could perhaps suggest a solution?
Thanks!
This problem is due to the data format problem caused by the special characters in description.
Kindly check if it can be solved by adding the correct encoding format into the -ContentType,
For example -ContentType 'application/json;charset=utf -8'

Request to VSTS REST API only works on Postman

I'm trying to run this request
curl -X POST \
'https://*****.visualstudio.com/DefaultCollection/_apis/wit/wiql?=&api-version=1.0' \
-H 'authorization: Basic *****' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: *****' \
-d '{
"query": "SELECT [System.Id] FROM WorkItems"
}'
but I keep getting this error
{"count":1,"value":{"Message":"A value is required but was not present in the request.\r\n"}}
It works as expected on Postman, so I think the request and the server are OK.
I'm trying to follow the first example shown here: https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql
Am I missing something?
The URL is wrong, remove =& from the REST API url and the url will be like this:
https://*****.visualstudio.com/DefaultCollection/_apis/wit/wiql?api-version=1.

Setting Query Parameter on MongoHQ beta API

I'm using the beta api on MongoHQ.com to query a database I set up earlier this week.
I can retrieve the list of databases on my account, the collections in each database and the documents in each collection using the basic URLs.
However, when I try to set a query, limit, fields, etc parameter, it's returning a 404 error.
This is the curl command I'm using:
curl -i -X GET 'https://beta-api.mongohq.com/mongo/<deployment id>/<database name>/collections/<collection name>/documents?query={"name": "Name"}' \
-H 'Content-Type: application/json' -H 'Authorization: api-key <API KEY>'
According to the documentation on the site here - https://docs.mongohq.com/mongohq-api-beta/mongodb-documents.html - the query has to be a "URL encoded MongoDB Extended JSON document". Unfortunately there are no examples of this.
How would I convert the query document above to a URL encoded one?
You can use curl to urlencode your query like this:
curl -i -X GET 'https://beta-api.mongohq.com/mongo/<deployment id>/<database name>/collections/<collection name>/documents' \
-G --data-urlencode 'query={"name": "Name"}' \
-H 'Content-Type: application/json' -H 'Authorization: api-key <API KEY>'
If you use operators like e.g. $gt, be sure to put double quotes around them:
curl -i -X GET 'https://beta-api.mongohq.com/mongo/<deployment id>/<database name>/collections/<collection name>/documents' \
-G --data-urlencode 'query={"value": { "$gt" : 10}}' \
-H 'Content-Type: application/json' -H 'Authorization: api-key <API KEY>'