开发者

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开发者_如何学Go, 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?


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}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜