我正在尝试使用 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/domainsI 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) 200I 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-urlencodedBut 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/domainsBut 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
发布评论