0

I need to do the following CURL request in python:

curl -X POST https://fake.url
-H 'apiKey: {apiKey}'
-H 'Content-Type: application/json'
-H 'Accept: application/json'
-d '{ "data": { "dirname": "{dirname}", "basename": "{filename}", "contentType": "application/octet-stream" } }'

I implemented this using the "requests" module installed via pip:

try:
    import requests
except ImportError:
    os.system("pip install requests")
    import requests

...

url = 'https://fake.url'

headers = {
    'apiKey': f'{apiKey}',
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}

payload = json.dumps({
    'data': {
        'dirname': f'{dirname}',
        'basename': f'{filename}',
        'contentType': 'application/octet-stream'
    }
})

response = requests.post(url, headers=headers, data=payload)

The customer requested NOT to use pip to install the "requests" module. Is it possible to do this? Or, is it possible to perform the CURL request without the "requests" module?

El_Merendero
  • 603
  • 3
  • 14
  • 28
  • 2
    use the inbuilt library `urllib3`. https://stackoverflow.com/questions/31778800/how-can-i-make-a-post-request-on-python-with-urllib3 – Equinox Sep 20 '20 at 15:21
  • 2
    You absolutely should not be running `pip install` from within the script itself. If requests isn't available, just fail with an error: "This script requires the requests module." Or use a stock module. Or call out to the `curl` command with `subprocess`. – larsks Sep 20 '20 at 15:21
  • 1
    I think you mean URL request, not CURL request (which is not a thing) - CURL is just one program that can do URL requests, but you don't need to use that. Would be nice to edit the question title and content if this is the case. – antont Sep 20 '20 at 15:36
  • 2
    @antont It's neither URL request. It's **HTTP request**! – phd Sep 20 '20 at 16:21

0 Answers0