0

I'm trying to run bash commands from python3 script and I get an error. Command:

#!/usr/bin/python3

import os

os.system('curl -k --header "Authorization: 3NKNRNNUrFQtu4YsER6" --header "Accept: application/json" --header "Content-Type: application/json" https://192.168.1.1/alerts/index/limit:1/page:1/sort:id/direction:desc.json |  jq -r ''{"request": {"alert": {"alert": .[0].alert, "new": "test"}}}'' > 1.json')

Error response:

jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at 
<top-level>, line 1:
{request:        
(23) Failed writing body
2
  • If you need single-quotes inside your single-quoted string, you need to escape them: \' Commented Oct 10, 2018 at 11:51
  • 1
    Use a library like requests instead of running curl. Commented Oct 10, 2018 at 11:52

1 Answer 1

1

There's no need to use curl and jq; Python has libraries to handle both HTTP requests and JSON data. (requests is a 3rd-party library; json is part of the standard library.)

import json
import requests

with open("1.json", "w") as fh:
    response = requests.get("https://192.168.1.1/alerts/index/limit:1/page:1/sort:id/direction:desc.json",
                            headers={"Accept": "application/json",
                                     "Content-Type": "application/json",
                                     "Authorization": "3NKNRNNUrFQtu4YsER6"
                                    }
                           ).json()
    json.dump(fh, {'request': {'alert': {'alert': response[0]['alert'], 'new': 'test'}}})

If you insist on using curl and jq, use the subprocess module instead of os.system.

p = subprocess.Popen(["curl", "-k",
                      "--header", "Authorization: 3NKNRNNUrFQtu4YsER6",
                      "--header", "Accept: application/json",
                      "--header", "Content-Type: application/json",
                      "https://192.168.1.1/alerts/index/limit:1/page:1/sort:id/direction:desc.json"
                     ], stdout=subprocess.PIPE)

with open("1.json", "w") as fh:
    subprocess.call(["jq", "-r", '{request: {alert: {alert: .[0].alert, new: "test"}}}'], 
                    stdin=p.stdout,
                    stdout=fh)
Sign up to request clarification or add additional context in comments.

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.