Summary
Postman is a way to easily construct and test requests against an API.
Configuring with Discourse
-
After opening Postman, click Create Workspace:
-
Choose
Blank workspace
and click Next. -
Title it
Discourse API
and select Create: -
Click
Collections
:
-
Click Import:
-
Paste
https://docs.discourse.org/openapi.json
(which is the “Download” URL shown on docs.discourse.org):
-
Choose to import as a
Postman Collection
:
Create a new Environment
-
Click on
Environments
andCreate Environment
. This will hold variables specific to the instance you’re testing. For this example, we’ll usetry.discourse.org
.- add a new variable:
defaultHost
with the valuetry.discourse.org
: - select the
try.discourse.org
environment from the ⑤ Environment dropdown
- add a new variable:
Hello World!
Let’s try a request!
- Select
Collections
→categories.json
→Retrieves a list of categories
and click Send. You should see the result:
Add authentication to requests
To use the API with API key authentication:
-
Add the following variables to this or a new environment:
api-username
api-key
(their values must of course be valid for the site you are querying) -
Add a script to the collection
SelectCollections
→Discourse API Documentation
→Scripts
→Pre-request
and paste the following:pm.request.headers.add({ key: "api-key", value: pm.variables.get("api-key") }) pm.request.headers.add({ key: "api-username", value: pm.variables.get("api-username") })