1

I am accessing the subprocess module to call a shell function. Part of the function call is a string:

data = '\'{"data": [{"content": "blabla"}]}\'' 

when passing the string, I get the following error:

from subprocess import check_output check_output(['curl', '-d', data, 'http://service.location.com'], shell=True) Error: raise CalledProcessError(retcode, cmd, output=output) ... returned non-zero exit status 2 

I actually know the problem, it's that the string gets passed the way it looks to Python, escapes and all.

Using the console,

$ curl -d \'{"data": [{"content": "blabla"}]}\' http://service.location.com 

gives the same error, while

$ curl -d '{"data": [{"content": "blabla"}]}' http://service.location.com 

runs perfectly. Any ideas how to tell Python that it passes a string .. fully converted?

2
  • Have you tried data = '{"data": [{"content": "blabla"}]}' instead of data = '\'{"data": [{"content": "blabla"}]}\''? Commented Mar 27, 2015 at 2:17
  • Yes, unfortunately both ' and " are required parts of the input format. Commented Mar 27, 2015 at 15:30

2 Answers 2

4

When you use shell=True parameter, you don't need to split your actual command.

>>> check_output('''curl -d '{"data": [{"content": "blabla"}]}' http://service.location.com''', shell=True) b'<!DOCTYPE html>\n<!--[if lt IE 7]> <html class="location no-js lt-ie9 lt-ie8 lt-ie7" lang="en" ng-app="homeapp" ng-controller="AppCtrl"> <![endif]-->\n<!--[if IE 7]> <html class="location no-js lt-ie9 lt-ie8" lang="en" ng-app="homeapp" ng-controller="AppCtrl"> <![endif]-->\n<!--[if IE 8]> <html class="location no-js lt-ie9" lang="en" ng-app="homeapp" ng-controller="AppCtrl"> <![endif]-->\n<!--[if gt IE 8]><!--> <html class="location no-js" ng-app="homeapp" ng-controller="AppCtrl"> <!--<![endif]-->\n\n<head>\n <title>Location.com\xe2\x84\xa2 | Real Estate Locations for Sale and Rent</title>\n <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /><![endif]-->\n <meta charset="utf-8">\n\n <link rel="dns-prefetch" href="//ajax.googleapis.com" />\n 

OR

>>> data = """'{"data": [{"content": "blabla"}]}'""" >>> check_output('''curl -d {0} http://service.location.com'''.format(data), shell=True) 
Sign up to request clarification or add additional context in comments.

Comments

1

Remove Shell=True, try this:

data = '{"data": [{"content": "blabla"}]}' from subprocess import check_output check_output(['curl', '-d', data, 'http://service.location.com']) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.