Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Curl "-I --user" to Python Requests

Tags:

I'm trying to convert a cURL command to python and I'm struggling

curl -I --user username:password https://an.api.on.the.internet/ 

My current attempt is:

import requests cur = requests.get('https://an.api.on.the.internet', auth='username:password') 

Could anyone help me convert it? thanks

like image 909
Marcus Avatar asked Aug 21 '13 17:08

Marcus


People also ask

How do you convert curl to code in Python?

Curl Converter automatically generates valid Python code using the Python request library for all provided Curl HTTP headers and Curl data. Enter the Curl command, click Run to execute the command online and check the results. Click Generate Code and select Python to convert the Curl command to Python code.

Does Python request use curl?

In Python, cURL transfers requests and data to and from servers using PycURL. PycURL functions as an interface for the libcURL library within Python. Almost every programming language can use REST APIs to access an endpoint hosted on a web server.


1 Answers

Use

requests.get(url, auth=(username, password)) 

See the section on Basic Authentication in the requests documentation.

like image 72
Lukas Graf Avatar answered Oct 07 '22 01:10

Lukas Graf