1

enter image description here

I have been writing a Python script to read from a file that contains multiple curl commands and then make HTTP requests to get responses (JSON).

I have done the step of reading the file and saving it to a curl array. Then I'm stuck at the step of using that curl array to make HTTP requests to get responses. Is there any Python library that supports a function that receives a curl string as a parameter and returns an HTTP response?

I have found one solution on StackOverflow is to use "subprocess" to execute the command but its drawback is that it requires "curl" to be installed on the machine which runs the script:

def call_curl(curl):
    args = shlex.split(curl)
    process = subprocess.Popen(args, shell=False, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    return json.loads(stdout.decode('utf-8'))
Ben Alpha
  • 33
  • 2
  • 5
  • 1
    Does this answer your question? [How to use Python to execute a cURL command?](https://stackoverflow.com/questions/25491090/how-to-use-python-to-execute-a-curl-command) – pii_ke May 20 '22 at 04:58
  • @pii_ke I think this solution is the same as the function I mention in my question. – Ben Alpha May 20 '22 at 07:33
  • 1
    I don't expect such library to exist, and I think googling would be enough for that. You'll have to resort to parsing command lines by yourself, I'm afraid. – Nikolaj Š. May 20 '22 at 08:10

2 Answers2

2

Try using uncurl to parse curl command and then use requests to fetch the resource:

import uncurl
import requests

ctx = uncurl.parse_context("curl 'https://pypi.python.org/pypi/uncurl' -H 'Accept-Encoding: gzip,deflate,sdch'")

r = requests.request(ctx.method.upper(), ctx.url, data=ctx.data, cookies=ctx.cookies, headers=ctx.headers, auth=ctx.auth, verify=(not ctx.insecure))

r.status_code

see https://docs.python-requests.org/en/latest/user/advanced/#custom-verbs and https://github.com/spulec/uncurl/blob/master/uncurl/api.py#L107 to get some idea about how the code works.

pii_ke
  • 2,811
  • 2
  • 20
  • 30
  • This is the library I need. But it seems it's not very stable yet. I have this valid curl command: "curl --location --request GET 'localhost:8000/cs/api/user/info/?user_id=c0f8f66d-bd4e-4305-a536-6de3b2da926c' --header 'Authorization: Token 92c7c270bd43e79a6546d8b80de1c1ce4e593724' --header 'Cookie: csrftoken=tmbcb5KPaZFBU4PavGa4Z5hTrrbcL1eSlgrxGVqXsHV8njXVuIRZdxJK1NVmLvgP'". I passed it to uncurl.parse_context() and then it throws an error. – Ben Alpha May 20 '22 at 09:11
  • Yes. it looks like it doesn't support `--request` and `--location` flags. I might try to add support to these arguments later. https://github.com/spulec/uncurl/blob/master/uncurl/api.py#L21 – pii_ke May 20 '22 at 09:31
-1

If you're using Python, you don't need cURL at all, as HTTP clients exist for Python, like Requests.

ndc85430
  • 1,395
  • 3
  • 11
  • 17
  • I know we can use "requests" to make an HTTP request in Python. But in my case, I need to read curl commands from a file and process them. – Ben Alpha May 20 '22 at 07:30