Search code examples
pythonpython-3.xpython-requestsmarkdowngithub-api

How to use the Github Markdown API using Python


I'm trying to use the Github Markdown API to turn Markdown files into pretty HTML files but I can't seem to get the API to work.

I've been using requests.post with payloads exactly as detailed in the documentation here and I've tried changing a few things but nothing seems to return the HTML that I want.

Here is the code I'm using:

import requests

with open("index.md", "r") as markdown, open("index.html", "w") as html:
    payload = {"text": markdown.read(), "mode": "markdown"}
    html.write(requests.post("https://api.github.com/markdown", data=payload).text)

The return from the Github API is the following:

{
    "message": "Problems parsing JSON",
    "documentation_url": "https://developer.github.com/v3/markdown/#render-an-arbitrary-markdown-document"
}

where an HTML version of my document is expected back.


Solution

  • Since the API was expecting json the request should have been:

    html.write(requests.post("https://api.github.com/markdown", json=payload).text)
    

    Which uses json instead of data to send the payload with the post request.