GET 请求通过 Python 而不是通过 Postman

编程入门 行业动态 更新时间:2024-10-27 14:28:55
本文介绍了GET 请求通过 Python 而不是通过 Postman的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 Mailman 3 REST API,但我需要从 java 类中的 Spring 的 Rest 模板调用它,或者为了测试目的从 Postman 调用它.在 Python 中,我可以通过以下方式调用 API:

>>>从 httplib2 导入 Http>>>标题 = {...内容类型":应用程序/x-www-form-urlencode",...授权":基本 cmVzdGFkbWluOnJlc3RwYXNz",... }>>>url = 'localhost:8001/3.0/domains'>>>响应,内容 = Http().request(url, 'GET', None, headers)>>>打印(响应.状态)200

我想提出同样的要求,但要通过 Postman.为此,我使用了 URL "127.0.0.1:8001/3.0/domains".我在标题中添加了 2 个字段,预览效果如下:

GET/3.0/lists HTTP/1.1主机:127.0.0.1:8001内容类型:应用程序/x-form-urlencode授权:基本 cmVzdGFkbWluOnJlc3RwYXNz缓存控制:无缓存内容类型:应用程序/x-www-form-urlencoded

但是请求状态只显示待处理",我没有收到任何回复.Mailman 在 python virtualenv 上运行.

我还尝试在终端中运行以下命令:

curl --header "Content-Type: application/x-form-urlencoded, Authorization: Basic cmVzdGFkbWluOnJlc3RwYXNz" localhost:8001/3.0/domains

但输出是:

{"title": "401 未经授权","description": "REST API 需要身份验证"}

我被困在这一点上,因为我不希望使用 Mailman 的 Web UI postorius,但希望从 java 类调用 REST API.我也想知道,如果我在形成 Postman 请求时犯了一个特别的错误,但是我如何使用 Spring 的 Rest 模板来做同样的事情.

任何帮助将不胜感激.谢谢

解决方案

要修复 curl 命令,请单独使用 -H 参数.

curl -H "Content-Type: application/x-form-urlencoded" -H "Authorization: Basic cmVzdGFkbWluOnJlc3RwYXNz" localhost:8001/3.0/domains

I am trying to use the Mailman 3 REST API, but I need to call it from Spring's Rest Template in a java class, or for testing purpose from Postman. In Python, I can call the API by:

>>> from httplib2 import Http >>> headers = { ... 'Content-Type': 'application/x-www-form-urlencode', ... 'Authorization': 'Basic cmVzdGFkbWluOnJlc3RwYXNz', ... } >>> url = 'localhost:8001/3.0/domains' >>> response, content = Http().request(url, 'GET', None, headers) >>> print(response.status) 200

I want to make the same request, but through Postman. For that, I have used the URL "127.0.0.1:8001/3.0/domains". I have added 2 fields in the headers, the preview looks like:

GET /3.0/lists HTTP/1.1 Host: 127.0.0.1:8001 Content-Type: application/x-form-urlencode Authorization: Basic cmVzdGFkbWluOnJlc3RwYXNz Cache-Control: no-cache Content-Type: application/x-www-form-urlencoded

But the request status only shows "pending" and I receive no response. Mailman is running on a python virtualenv.

I am also trying to run the following command in my terminal:

curl --header "Content-Type: application/x-form-urlencoded, Authorization: Basic cmVzdGFkbWluOnJlc3RwYXNz" localhost:8001/3.0/domains

But the output is:

{ "title": "401 Unauthorized", "description": "The REST API requires authentication" }

I am stuck at this point, because I do not wish to use Mailman's Web UI postorius, but wish to call the REST API from java class. I would also like to know, if I am making a particular mistake in forming my Postman request, but how can I do the same using Spring's Rest Template.

Any help would be appreciated. Thank you

解决方案

To fix curl command, use the -H parameter seperately.

curl -H "Content-Type: application/x-form-urlencoded" -H "Authorization: Basic cmVzdGFkbWluOnJlc3RwYXNz" localhost:8001/3.0/domains

更多推荐

GET 请求通过 Python 而不是通过 Postman

本文发布于:2023-10-29 13:24:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539807.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   Python   Postman

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!