1

If I make the request

api-key = 'asdfklhsdfkjahsdlgkjahlkdjahfsa'
url = 'https://www.website.com'

headers = {'api-key': api-key,
           'Content-Type': 'application/json'}

request_data = {'foo': 'bar', 'egg': 'spam'}

result = requests.post(url, headers=headers, data=request_data)

The server is contacted. Suppose that instead I want to do something like

request_string = requests.foobar(url, headers=headers, data=request_data)
import os
os.system('curl ' + request_string)

So that I can look to see what the request is doing without bothering the server (possibly to the point that I could c&p it into curl), what would foobar be? Or in general, what is a way to inspect the contents of the request without making it?

Frank Harris
  • 587
  • 2
  • 16
  • 1
    Does this help answer your question? https://stackoverflow.com/questions/20658572/python-requests-print-entire-http-request-raw – holts-shoe Sep 22 '22 at 19:53
  • 1
    @holts-shoe Almost, thanks! There are complicated solutions there but `result = requests.post(...); print(result.request)` is very useful. The only problem there is the request seems to still be made, which I'm trying to avoid. – Frank Harris Sep 22 '22 at 20:16

1 Answers1

0

Here's another post that implies that you can use Request().prepare() to observe the request without actually sending the request.

Furthermore the official documentation reads "In some cases you may wish to do some extra work to the body or headers (or anything else really) before sending a request. The simple recipe for this is the following" and then it illustrates Request.prepare()

holts-shoe
  • 81
  • 7