How to send Python variable to curl command for terminal in Google Colaboratory (quotes and '$' doesn't work for curl)

SantoshGupta7 :

This is a followup to this Stackoverflow post

How to use Python variables in Google Colab terminal command?

Which asks how to send python variables to the command line.

The answer is you need to place a $ in front of it. In other cases, you need to quote everything like this '$variable'

This works for me except for curl commands. For example

emailBody1 = ' this is some test text'

!curl -X POST 'http://localhost.yxy' -H "Content-Type: application/json" -d '{"emails": [{"emailBody": '$emailBody1'}}'

I tried no quotes, single quotes, and double quotes. No quotes results in an error, and single/double quotes just sends whatever is inside as a string; no reference to the variable.

furas :

This works for me

-d  '{{"emails": [{{"emailBody": "$emailBody1" }}]}}'

or

-d  '{{"emails": [{{"emailBody": "{emailBody1}" }}]}}'

All string is inside ' ' (or " ") without spliting to 'string' $var 'string'

And normal { } has to be converted to {{ }} because it uses { } to put values from variables.


I tested it with http://httpbin.org/post which sends back all data so I could see what was send.

emailBody1 = ' this is some test text'

!curl -X POST 'http://httpbin.org/post' -H "Content-Type: application/json" -d  '{{"emails": [{{"emailBody": "{emailBody1}" }}]}}'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8502&siteId=1