0

I am aware there exists questions answered on this but I wanted to ask as it is different from the rest.

I am trying to send a curl request using subprocess and cannot get a variable to funcion:

 config = json.loads(open('/etc/config.json').read())
    endpoint = config["end_point"]
    auth = config["auth_token"]
    config["widgets"]



    #Send using subprocess
    import subprocess
    subprocess.call([
    'curl',
    '-X',
    'POST',
    '-H',
    'Content-Type: application/json',
    '-d',
    '{ "auth_token": **'auth'**, "widget": "329bdbea887ad8e10e4e496f7a60f898", "title": "Something", "items":[{"label": "testR", "value": "$999.99"}, {"label": "testB", "value": "$9,001.00"}] }',
    **'endpoint'**

The config file contains the following:

{"end_point":"http://domain.com","auth_token":"e91a0ffe758c19=ed0","widgets":{"host_info":"fd8a63ads499289a312","memory_info":"0bc2c24ds28bc4c507ad","cpu_info":"5assd0dc41dbd73681"}}

Neither auth not endpointurl is not working.

Full code is:

import subprocess subprocess.call([ 'curl', '-X', 'POST', '-H', 'Content-Type: application/json', '-d', '{ "auth_token": "e91a0ffe758c194896eb4daed0", "widget": "329bdbea887ad8496f7a60f898", "title": "Something", "items":[{"label": "text2, "value": "$999.99"}, {"label": "text1", "value": "$9,001.00"}] }', 'http://domain.com' ])

I

Here I am trying to use auth variable instead of "e91a0ffe758c194896eb4daed0"

Spanglish
  • 119
  • 1
  • 2
  • 13

1 Answers1

0

The line

config["widgets"]

isn't doing anything, it's not clear what you want here. As far as I can tell, what you mean by:

'{ "auth_token": 'auth', "widget": "329bdbea887ad8e10e4e496f7a60f898", "title": "Something", "items":[{"label": "testR", "value": "$999.99"}, {"label": "testB", "value": "$9,001.00"}] }',

is to pass some JSON to cURL inside of a string. What you're looking for, then, is:

'{ "auth_token": "%s", "widget": "329bdbea887ad8e10e4e496f7a60f898", "title": "Something", "items":[{"label": "testR", "value": "$999.99"}, {"label": "testB", "value": "$9,001.00"}] }' % auth

Also, 'endpoint' means 'the string containing the characters 'e', 'n', 'd', 'p', 'o', 'i', 'n', 't', not your variable, endpoint. You probably just want to drop the quotes there. You haven't pasted all of your code -- seeing as the parentheses are unbalanced -- so we can't be sure of what's going on, but that's probably a start.

EDIT: Try again, I made a mistake -- string.format won't work because of the {} characters inside your string. Use the corrected % solution above.

Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
  • Thanks for the reply. Sorry if it is not clear. Let me explain you once again in case you have doubts. What i want is basicly pass a variable in this case "auth" inside the subprocess.call. "auth_token": "{}", is not doing it for me for the values are inside '. How can I assign auth to "auth_token" value in the subprocess call? – Spanglish Dec 30 '13 at 12:24
  • Please post the full code that you are attempting to use in your question. – Patrick Collins Dec 30 '13 at 12:33
  • What error are you getting when you try the code that I just posted? – Patrick Collins Dec 30 '13 at 12:41
  • What is the value you're expecting? What is the value you're getting? – Patrick Collins Dec 30 '13 at 12:47
  • The value I am expecting should be the value of auth, thats clear: @peter check here: http://pastebin.com/W46D4i0R – Spanglish Dec 30 '13 at 14:17