Search code examples
pythoncurlpycurl

Equivalent python code for curl command for https call


I have a curl command that works and gives me back the JSON. Curl command:

    curl -sS -k -L -H "Authorization: bearer <token>" -X GET https://IP:PORT/api/v1/namespaces

I tried with requestsand pycurl modules which I found in the other posts but no luck.

Can anyone help me with finding the equivalent in python???


Solution

  • We can do this with requests like this:

    import requests
    header = {'Authorization': 'bearer <token>'}
    resp = requests.get("https://IP:PORT/api/v1/namespaces", 
        headers = header, 
        verify=False)
    print(resp.status_code)
    print(resp.text)
    
    • The -H switch behaviour is replicated by sending a header
    • The -L switch behaviour is replicated by specifying verify=False
    • The -sS and -k are about the behaviour of curl and not relevant here
    • The -X GET is replicated by using requests.get()