使用 Python 中的请求包通过 API 密钥调用 REST API [英] Calling REST API with an API key using the requests package in Python

查看:33
本文介绍了使用 Python 中的请求包通过 API 密钥调用 REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 requests 包调用下面的 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 like code. When you do not know what it supports, 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 = {'file': open('filename', 'rb')}

req = requests.get(url, headers=headers, auth=auth, files=files)

这篇关于使用 Python 中的请求包通过 API 密钥调用 REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆