1

I have the following code:

  import subprocess
    cmd = curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "79c08a7e70f0253c3da2fab39e7cb89b", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://collector.superviso.com
    subprocess.call(cmd)

I am trying to assign value to a variable which results in syntax error. Here is what happens:

>>> cmd = curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth_token", "widget": "widget_id", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://domain.com
  File "<stdin>", line 1
    cmd = curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth_token", "widget": "widget_id", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://domain.com
                     ^
SyntaxError: invalid syntax

Thank you in advance.

UPDATE01

The triple quote string does let me assign the value yet subproces not working

>>> cmd = """curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "329bdbea887ad8e10e4e496f7a60f898", "title": "Something", "items":[{"label": "BOUGHT BREAD FOR", "value": "$999.99"}, {"label": "SOLD WATER FOR", "value": "$9,001.00"}] }' http://collector.superviso.com """
>>> subprocess.call(cmd)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 486, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1201, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

UPDATE02

Thanks to Adam`s post(see above) I was able to sedn the request without assigning any variable:

subprocess.call([
    'curl',
    '-X',
    'POST',
    '-H',
    'Content-Type: application/json',
    '-d',
    '{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "79c08a7e70f0253c3da2fab39e7cb89b", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }',
    'http://collector.superviso.com'
    ])
Spanglish
  • 119
  • 1
  • 2
  • 13

3 Answers3

3

That doesn't work even without special characters:

>>> cmd = curl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'curl' is not defined

You're trying to create a string, so you have to use some variant of the string literal syntax:

>>> cmd = 'curl'

That makes spaces acceptable, and double quotes:

>>> cmd = 'curl -X POST -H "Content-Type: application/json"'

However, you can't nest unescaped single quotes in that. To deal with that, your options are to escape the internal single quotes or to triple-quote the whole string:

>>> cmd = 'curl -X post -H "Content-Type: application/json" -d \'{ "auth_token"...'

>>> cmd = """curl -X POST -H "Content-Type: application/json" -d '{ "auth_token"..."""
Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • +1. and to call `cmd` as a subprocess: `subprocess.check_call(shlex.split(cmd))` – jfs Dec 05 '13 at 18:19
3

subprocess.call also takes a list of arguments instead of a single, flat argument string. See the subprocess documentation. It's much easier to use that than worry about proper quoting. For example:

subprocess.call([
    'curl',
    '-X',
    'POST',
    '-H',
    'Content-Type: application/json',
    '-d',
    '{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "79c08a7e70f0253c3da2fab39e7cb89b", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }',
    'http://collector.superviso.com'
    ])
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • You could also use this as inspiration for another way to create the string literal even if the `subprocess` module didn't - `' '.join(['curl', '-X', ...])` – Peter DeGlopper Dec 05 '13 at 20:29
  • I should congratulate Adam for taking attention to this method. It solved the problem!! – Spanglish Dec 06 '13 at 07:34
  • @user2843053: Welcome to StackOverflow. If this solved your problem, you should [accept it as the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Adam Rosenfield Dec 06 '13 at 15:39
2

You want a raw triple double-quoted string r"""blah""".

For more details, see this How to include a quote in a raw Python string?

Community
  • 1
  • 1
William Denman
  • 3,046
  • 32
  • 34