简体   繁体   English

查看 Python 脚本发出的 POST 请求的标头和正文

[英]View headers and body of a POST request made by Python Script

In my application, I have my API that is in localhost:8000/api/v0.1/save_with_post .在我的应用程序中,我的 API 位于localhost:8000/api/v0.1/save_with_post中。

I've also made a Python Script in order to do a Post Request on such Api.我还制作了一个 Python 脚本,以便对此类 Api 执行发布请求。

### My script
import requests

url = 'localhost:8000/api/v0.1/save_with_post'
myobj = {'key': 'value'}

x = requests.post(url, data = myobj)

Is it possible to view headers and body of the request in Chrome rather than debugging my application code?是否可以在 Chrome 中查看请求的标头和正文,而不是调试我的应用程序代码?

You want Postman .你想要Postman

With Postman you can either generate a request to your service from Postman itself, or set up Postman as a proxy so you can see the requests that your API client is generating and the responses from the server. With Postman you can either generate a request to your service from Postman itself, or set up Postman as a proxy so you can see the requests that your API client is generating and the responses from the server.

If you want to view the response headers from the post request, have you tried:如果您想查看 post 请求中的响应标头,您是否尝试过:

>>> x.headers

Or you could just add headers yourself to your POST request as so:或者您可以自己将标头添加到您的 POST 请求中,如下所示:

h = {"Content-Type": "application/xml", ("etc")}
x = requests.post(url, data = myobj, headers = h)

well, I don't know if there is a way you could view the request in Chrome DevTools directly (I don't think there is) however, I know there are two alternatives for seeing the request body and response:好吧,我不知道是否有一种方法可以直接在 Chrome DevTools 中查看请求(我认为没有)但是,我知道查看请求正文和响应有两种选择:

1 - use selenium with a chrome webdriver 1 - 使用 selenium 和 chrome webdriver
this will allow you to run chrome automated by python.这将允许您运行由 python 自动运行的 chrome。 then you can open a test page and run javascript in it to do your post request, see this for more info on how to do this:然后您可以打开一个测试页面并在其中运行 javascript 来执行您的发布请求,请参阅此以获取有关如何执行此操作的更多信息:

you will need to use Selenium-requests library to use requests library with selenium您将需要使用 Selenium-requests 库来使用带有 selenium 的请求库

2 - use Wireshark 2 - 使用 Wireshark
this program will allow you to see all the traffic that is going on your network card and therefore you will be able to monitor all the requests going back and forth.该程序将允许您查看网卡上的所有流量,因此您将能够监控所有来回的请求。 however, Wireshark will throw all the traffic that you network card send or receives it may be hard to see the specific request you want但是,Wireshark 会抛出您网卡发送或接收的所有流量,可能很难看到您想要的特定请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM