问题
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
回答1:
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
来源:https://stackoverflow.com/questions/36052940/get-request-working-through-python-but-not-through-postman