Search code examples
pythontwistedtwisted.web

Post request with multiple parameters using Twisted Web Client


I would like to send a POST request with multiple parameters using Twisted Web Client :

  • image : image
  • metadata : json document with meta data I need to use pure Twisted without external libraries like Treq and requests.

At the moment, I can send only one parameter and tried few ways without success.

Do someone know how to change body to achieve this goal ?

from __future__ import print_function

from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

from bytesprod import BytesProducer

agent = Agent(reactor)
body = BytesProducer(b"hello, world")
d = agent.request(
    b'POST',
    b'http://httpbin.org/post',
    Headers({'User-Agent': ['Twisted Web Client Example'],
             'Content-Type': ['text/x-greeting']}),
    body)

def cbResponse(ignored):
    print('Response received')
d.addCallback(cbResponse)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

Solution

  • You need to specify how you would like the parameters encoded. If you want to to submit them like a browser form, you need to application/x-www-form-urlencoded or multipart/form-data encode the data. The former is generally for short data - and since of your parameters is "image" it probably isn't short. So you should multipart/form-data the data.

    Once you have, you just declare this in the request head and include the encoded data in the body.

    For example,

    body = multipart_form_encoded_body_producer(your_form_fields))
    d = agent.request(
        b'POST',
        b'http://httpbin.org/post',
        Headers({'User-Agent': ['Twisted Web Client Example'],
                 'Content-Type': ['multipart/form-data']}),
        body)
    

    Conveniently, treq provides a multipart/form-data encoder

    So multipart_form_encoded_body_producer(...) probably looks something like:

    MultiPartProducer([
        ("image", image_data),
        ("metadata", some_metadata),
        ...
    ])
       
    

    You mentioned that you can't use Treq. You didn't mention why. I recommend using Treq or at least finding another library that can do the encoding for you. If you can't do that for some unreasonable reason, you'll have to implement multipart/form-data encoding yourself. It is reasonably well documented and of course there are multiple implementations you can also use as references and interoperability testing tools.