torpy icon indicating copy to clipboard operation
torpy copied to clipboard

Headers, params, data similar to "requests" format?

Open hess8 opened this issue 3 years ago • 1 comments

How can headers, params, data be passed as in for the traditional "requests" library? e.g. requests.post(url, headers=headers, params=params, data=data)

It seems I can't use this format trying sess.post(url, headers=headers, params=params, data=data)

hess8 avatar Mar 03 '22 23:03 hess8

#!/usr/bin/python

import requests
from torpy import TorClient
from torpy.http.adapter import TorHttpAdapter


with TorClient() as tor:
    with tor.get_guard() as guard:
        adapter = TorHttpAdapter(guard, 3)
        with requests.Session() as session:
            session.mount('http://', adapter)
            session.mount('https://', adapter)
            url = 'https://httpbin.org/post'
            headers = {
                'User-Agent': 'python-requests/2.27.1',
                'Accept-Encoding': 'gzip, deflate, br',
                'Accept': '*/*',
                'Connection': 'keep-alive'}
            params = {'example': 'parameter'}
            data = {'text': 'Hi there!'}
            request = session.post(url, headers=headers,
                                   params=params, data=data)
            print(request.text.rstrip())
{
  "args": {
    "example": "parameter"
  },
  "data": "",
  "files": {},
  "form": {
    "text": "Hi there!"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Length": "16",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.27.1",
    "X-Amzn-Trace-Id": "Root=1-62918379-796fdf621240496316d389d0"
  },
  "json": null,
  "origin": "107.189.1.160",
  "url": "https://httpbin.org/post?example=parameter"
}

konomikitten avatar May 28 '22 02:05 konomikitten

@konomikitten thanks for example

jbrown299 avatar Aug 24 '22 06:08 jbrown299