Home:ALL Converter>Post request with multiple parameters using Twisted Web Client

Post request with multiple parameters using Twisted Web Client

Ask Time:2021-01-04T17:02:58         Author:hotips

Json Formatter

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()

Author:hotips,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65560047/post-request-with-multiple-parameters-using-twisted-web-client
yy