Github-api giving 404 when passing json-data with python + urllib2

后端 未结 4 1030
离开以前
离开以前 2020-12-21 04:31

I have the following code, which should perform the first part of creating a new download at github. It should send the json-data with POST.

jsonstring = \'{         


        
4条回答
  •  眼角桃花
    2020-12-21 05:01

    The v3 github api has a nice feature where it can convert markdown to html. No authentication is needed to request this information, hence it makes a nice example to try before delving into the more tricky world of authentication.

    The API docs state:

    Render an arbritrary Markdown document

    POST /markdown Input

    text Required string - The Markdown text to render

    And even give an example of a markdown string to be converted:

    {"text": "Hello world github/linguist#1 **cool**, and #1!"}
    

    Given this information, lets build an urllib2 Request for the html-ified version of that markdown.

    import urllib2
    import json
    
    
    data = {"text": "Hello world github/linguist#1 **cool**, and #1!"}
    json_data = json.dumps(data)
    
    req = urllib2.Request("https://api.github.com/markdown")
    result = urllib2.urlopen(req, json_data)
    
    print '\n'.join(result.readlines())
    

    And the result is some html representing the markdown.

    Given this example, it is not the JSON which is causing the 404 when posting your request, but more likely the authentication (as you have already answered).

提交回复
热议问题