如何将curl命令转换为请求 [英] How to convert curl command to Requests

查看:71
本文介绍了如何将curl命令转换为请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从REST API检索数据.

I need to retrieve data from a REST API.

在Centos shell中,我可以这样做:

In the Centos shell I can do:

 curl -H "ID:1234" -H "Password:ABC" http://url.com/curl

我正在尝试使用Python中的请求执行此操作.

I am trying to do this with Requests in Python.

快速入门页面上,我看到了:

 payload = {'ID': '1234' , 'Password' : 'ABC' }
 requests.get("http://url.com/curl", params=payload)

但是,这不起作用.它只返回状态200,但没有数据.

However this doesn't work. It only returns the status 200, but no data.

推荐答案

使用 -H 用于curl的句柄,您可以设置标题值.在请求中,您传递标头数据的方式与处理参数相同,但是您使用了不同的关键字.

With the -H handle for curl, you're setting header values. In requests, you pass header data the same way you do with the params but you use a different keyword.

headers = {'ID': '1234' , 'Password' : 'ABC' }
requests.get("http://url.com/curl", headers=headers)

从请求文档中查看自定义标题

这篇关于如何将curl命令转换为请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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