GET 请求通过 Python 而不是通过 Postman [英] GET request working through Python but not through Postman

查看:69
本文介绍了GET 请求通过 Python 而不是通过 Postman的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想提出同样的要求,但要通过 Postman.为此,我使用了 URL "http://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" http://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" http://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 = 'http://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 "http://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" http://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" http://localhost:8001/3.0/domains

这篇关于GET 请求通过 Python 而不是通过 Postman的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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