使用Python中的request包使用API密钥调用REST API [英] Calling REST API with an API key using the requests package in Python
本文介绍了使用Python中的request包使用API密钥调用REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用请求包调用下面的REST API的python代码应该是什么?我不知道如何通过"apikey"
What should the python code to call the REST API below using the requests package? I do not know how to pass the "apikey"
curl -X POST -u "apikey":"1234abcd" -H "Accept: application/json" -F "file=@{input_file}" https://api_url
谢谢您的帮助.
推荐答案
您的curl命令可能适用于代码.当您不知道它支持什么时.您可以curl --help
或使用curl ... --trace-ascii 1.txt
来确定过程
Your curl command is likely to the code. When you do not know what it supports to. You can curl --help
or use curl ... --trace-ascii 1.txt
to figure out the process
from requests.auth import HTTPBasicAuth
import requests
url = "https://api_url"
headers = {"Accept": "application/json"}
auth = HTTPBasicAuth('apikey', '1234abcd')
files = {'filename': open('filename','rb')}
req = requests.get(url, headers=headers , auth=auth , files=files)
这篇关于使用Python中的request包使用API密钥调用REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文