2

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.

12
  • 1
    on curl.trillworks.com you can convert curl command to python code with module requests and later use only python for this. Commented Feb 25, 2020 at 23:48
  • 1
    Oh -- yeah; in python as opposed to the Linux command line, there's no reason to use jq, or bash at all. Python has perfectly good network functionality. Use it. Commented Feb 25, 2020 at 23:54
  • "Immediate problem, though, is that emailBody1 = ' this is some test text' doesn't assign a variable emailBody at all. ". I am not trying to send a variable to "emailBody", which is the key. I am trying to assign a variable to the value, which is emailBody1 Commented Feb 25, 2020 at 23:54
  • 1
    There is absolutely no reason to be doing this. Use a library like requests instead of shelling out to curl. Commented Feb 25, 2020 at 23:55
  • 1
    ...which is to say, whatever magic in Jupyter tries to perform substitutions in shell commands presumably just doesn't understand shell syntax well enough to actually do it right. Nothing specific to curl. Commented Feb 26, 2020 at 0:02

2 Answers 2

3

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}" }}]}}'
Sign up to request clarification or add additional context in comments.

Comments

3

If you really insist on using curl for this, use subprocess to run a copy explicitly so you don't depend on Jupyter trying to do magic.

import subprocess, json
emailBody1 = ' this is some test text'

subprocess.run([
  'curl',
  '-X', 'POST',
  '-H', 'Content-Type: application/json',
  '-d', json.dumps({"emails": [{"emailBody": emailBody1}]}),
  'http://localhost.yxy',
])

This also has the advantage of using Python's json.dumps() to generate your JSON text, which won't break down in a number of cases where the other approach would (f/e, if your email text contains newlines, literal double quotes, or other special characters).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.