1. 程式人生 > 其它 >python中request中的params,data,json引數含義全解

python中request中的params,data,json引數含義全解

我們先來看下CTFLEARN中的114 challenge:POST Practice

This website requires authentication, via POST. However, it seems as if someone has defaced our site.Maybe there is still some way to authenticate? http://165.227.106.113/post.php

檢視http://165.227.106.113/post.php頁面原始碼,即可發現username: admin | password: 71urlkufpsdnlkadsf,簡單來說,此題需要我們向http://165.227.106.113/post.php,傳送post請求,將username和password引數提交,即可得到flag。

import requests

userinfo = {
    "username": "admin",
    "password": "71urlkufpsdnlkadsf"
}
# 方法1,使用data傳送,此題的正確答案
response = requests.post("http://165.227.106.113/post.php", data=userinfo)
print(response.text)

# 方法2: 使用json
# response = requests.post("http://165.227.106.113/post.php", json=userinfo)
#
print(response.text) # 方法3:直接在url上加引數 # response = requests.post("http://165.227.106.113/post.php?username=admin&password=71urlkufpsdnlkadsf") # print(response.text) # 方法4: 使用params引數,和方法3等同 # response = requests.post("http://165.227.106.113/post.php", params="username=admin&password=71urlkufpsdnlkadsf")
# print(response.text)

python中的四種方法有何區別呢?筆者guggle(csdn遠古某人)發現,這4種請求方式的區別在於:請求的url,請求header中Content-Type,請求的body不同。

方法1:使用data
url: http://165.227.106.113/post.php
header中的Content-Type:application/x-www-form-urlencoded
body:username=admin&password=71urlkufpsdnlkadsf

方法2:使用json
url: http://165.227.106.113/post.php
header中的Content-Type:application/json
body:{“username”: “admin”, “password”: “71urlkufpsdnlkadsf”}

方法3和方法4:直接在url上加引數和使用params
url: http://165.227.106.113/post.php?username=admin&password=71urlkufpsdnlkadsf
header中的Content-Type:沒有宣告
body:none
————————————————
版權宣告:本文為CSDN博主「遠古某人」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/guggle15/article/details/120362917