Search code examples
djangohttp-headershttp-postgoogle-url-shortener

Django creating/making POST request with headers


I want to make a POST request programmatically with a custom headers. How can I do that?

Actually i am trying to do this; use google shortener api. Maybe i misunderstood :/

Thanks you in advance :)


Solution

  • To send an POST request with Python and with headers you can do something like

    import urllib2
    import json
    data = {'data':'data'}
    url = api_url
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    request = urllib2.Request(url, data=json.dumps(data))
    request.add_header("Content-Type", "application/json") #Header, Value                                        
    opener.open(request)