/python/

python 3.4 - urllib - delete

2016-11-19 02:26:15

import urllib.request
import urllib.parse

print('----------- Delete Request -----------')

def delete(session_id, postId):

    print('!! Delete Item !!')

    headers = {
        'Content-Type': 'application/json;charset=UTF-8',
        'Authorization': ('Token %s' % session_id)
    }
    req = urllib.request.Request(
            'https://example.com/api/post/' + postId,
            headers=headers,
            method='DELETE'
    )

    try:
        response = urllib.request.urlopen(req)
        print("Success: " + response.read().decode('utf8'))
    except urllib.error.URLError as error:
        print("Error: " + error.read().decode('utf8'))

# -----------------------------------------------

sessionId = '454154345325368u5698'
postId = '1'

delete(sessionId, postId)