tl;dr;
Here’s the macro, example of it’s usage and the XSLT used to pretty print.
Background
We document our HTTP based services in Confluence and I wanted to…
- embed service invocations in wiki pages to show the result of calling a certain URL
- pretty print the output
- not have to write a Java plugin in Confluence
Steps
Making a HTTP request from a macro
I started of with the HTML macro and verified that it worked.
Formatting the response
Our services produces as compact XML as possible to save space, they don’t pretty print. Hence I needed to format the service response and after finding these XSLT’s I saved one of them as an attachment to a page within Confluence and paired it with the XSLT macro.
Alternating the URL displayed (and/or used to make the request)
As I’ve written about earlier we document all of our services in one space and selectively “publish” a subset of those to another space from which we allow export of PDF’s as the deliverable documentation. In order for this to work we need the service examples to show, and use, different domain names for accessing services based on wether the documentation is viewed in our internal space or in the space used to export PDF documentation to our clients. The logic for this is the first block in the macro in which it looks at which space the macro is rendered and selects the base URL based on that.
Caveats
Query parameters
Due to what appears to be a limitation or bug in the handling of user macros Confluence won’t properly render the macro if the URL to the service contains an equal (=) sign. So in order to support query parameters the macro replaces any occurrences of two colons (::) with an equal sign. It’s all illustrated in the Gist showing the macro.
What about JSON?
We use this solution for JSON as well but without the XSLT formatting stage which renders our JSON responses butt ugly, if you have a solution on how to format JSON as well – feel free to post a comment.