TIMEX
TIMEX

Reputation: 272274

Is this the right way to write a POST function in Python?

def URLRequest(url, params, method="POST"):
    h = Http()
    res, content = h.request(url, method, urllib.urlencode(params))
    r = {}
    r['res'] = res
    r['content'] = content
    return r

Suppose I want to POST {"key":"value"} to a url (REST). Is this function the right way to do it?

Upvotes: 1

Views: 168

Answers (1)

Björn Pollex
Björn Pollex

Reputation: 76876

If it works it is correct. You can make it shorter though:

def URLRequest(url, params, method="POST"):
    res, content = Http().request(url, method, urllib.urlencode(params))
    return {'res':res, 'content':content}

Upvotes: 2

Related Questions