Kazufumi Yoshimoto
Kazufumi Yoshimoto

Reputation: 1

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

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\":\"[email protected]\",\"firstname\":\"k\"}}'

but I want to substitute variables. So I want to use double quote.

Upvotes: 0

Views: 255

Answers (1)

Kazufumi Yoshimoto
Kazufumi Yoshimoto

Reputation: 1

I solved by myself.

  1. put double-quotes and backslash between variable.

  2. ConvertTo-Json

  3. 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

Upvotes: 0

Related Questions