2

Is there any way to perform PUT request in tornado httpclient?

For example are there any ways to replace urllib with Requests Library?

Or maybe subclass own client and inject there construction from this answer:

import urllib2
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request('http://example.org', data='your_put_data')
request.add_header('Content-Type', 'your/contenttype')
request.get_method = lambda: 'PUT'
url = opener.open(request)

Any painless patches, hacks, suggestions..

I want this construction to work propertly:

 response = yield gen.Task(http_client.fetch, opt.site_url + '/api/user/', method="PUT", body=urlencode(pdata))

For now it's not sending body.

Community
  • 1
  • 1
Nikolay Fominyh
  • 8,946
  • 8
  • 66
  • 102

1 Answers1

6

Nope, Tornado doesn't use urllib (presumably it blocks). The trick to using httpclient for anything more complicated than a basic GET is to create an HTTPRequest.

Untested, but should work:

from tornado.httpclient import HTTPRequest
request = HTTPRequest(opt.site_url + '/api/user/', method="PUT", body=urlencode(pdata))
response = yield gen.Task(http_client.fetch, request)
Cole Maclean
  • 5,627
  • 25
  • 37