Django Code Snippets

Powered by PythonAnywhere and Django 1.8.6

Google URL Shortner

Date Added: Nov. 11, 2015, 8:20 p.m.

Author: django-snippets


import json
import requests
def short_url(url):
    post_url = 'https://www.googleapis.com/urlshortener/v1/url?key={API_KEY}'
    params = json.dumps({'longUrl': url})
    response = requests.post(post_url,params,headers={'Content-Type': 'application/json'})
    return response.json()['id']

 

Tags: python, requests

Return to List