curlconverter / curlconverter

Transpile curl commands into Python, JavaScript and 27 other languages
https://curlconverter.com
MIT License
7.18k stars 871 forks source link

extract curl commands from pipelines #542

Closed DmitryShS closed 1 year ago

DmitryShS commented 1 year ago
curl -s -X POST \
  -H "Authorization: Token $TOKEN" \
  -H "Content-Type: application/json" \
  http://netbox/api/ipam/prefixes/ \
  --data '{"prefix": "192.0.2.0/24", "site": 6}' | jq '.'
verhovsky commented 1 year ago

The | jq '.' part at the end is the problem. If you remove it

curl -s -X POST \ -H "Authorization: Token $TOKEN" \ -H "Content-Type: application/json" \ http://netbox/api/ipam/prefixes/ \ --data '{"prefix": "192.0.2.0/24", "site": 6}'

the command converts just fine:

import os
import requests

headers = {
    'Authorization': 'Token ' + os.getenv('TOKEN', ''),
    # Already added when you pass json=
    # 'Content-Type': 'application/json',
}

json_data = {
    'prefix': '192.0.2.0/24',
    'site': 6,
}

response = requests.post('http://netbox/api/ipam/prefixes/', headers=headers, json=json_data)

You are piping the curl command's output to jq, we haven't implemented this because you could have a long chain of commands, like

foo | bar | baz | curl example.com | jq

so we have to search the pipeline for the curl command, and what do we do if there's more than one? But I guess we'll just take the first one and ignore the rest of the pipeline. In the future we could also set stdin/stdout to run those commands in the Python as well.