Search code examples
pythoncurlpython-clickdocopt

curl command to python dict


I am a backend dev at my current company. When the front-end team has an issue, they will sometimes use Chrome devtool's "copy request as curl" feature and send me the problematic curl request. I was thinking it would be cool if I could write a script to transform these curls in various ways -- removing, adding, or adjusting parameters, among other things.

The first step here I think would be parsing the command into something easier to work with. I was thinking about using something like click or docopt, but didn't see an easy command to do command line string -> intelligent python object or something like that. Ideally it would work not just in this case, but for any valid Unix command. Is there a lib that can do this easily? Or would I need to extend one of these libraries? Or am I better off just writing something from scratch?

Example of a curl I'm trying to parse:

curl http://myhost?myarg=ACTIVE&myotherarg=1 -H Pragma: no-cache -H Origin: http://localhost:5000 -H Accept-Encoding: gzip, deflate, sdch -H Accept-Language: en-US,en;q=0.8 -H Authorization: Token xxxxxxxx1234567 -H Accept: application/json, text/javascript, /; q=0.01 -H Cache-Control: no-cache -H User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36 -H Connection: keep-alive -H Referer: http://localhost:5000/projects/7/overview-all/active/ --compressed


Solution

  • Turns out the python module shlex can do most of the heavy lifting.

    I wrote some code to handle my particular use case: https://github.com/JesseAldridge/chrome_to_local_curl