欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中的post請求解讀

 更新時間:2024年11月18日 11:11:59   作者:dair6  
文章介紹了Python中POST請求的4種編碼方式:application/x-www-form-urlencoded、multipart/form-data、application/json和text/xml,通過代碼示例和工具使用,展示了如何發(fā)送和處理這些請求

python post請求

post請求有4中編碼方式

1.application/x-www-form-urlencoded

application/x-www-form-urlencoded是瀏覽器原生的form表單

提交的數(shù)據(jù)會按照key1=val1&key2=val2的格式,經(jīng)過url轉(zhuǎn)碼,然后傳輸

(1)發(fā)送post請求

我們除了可以直接編寫代碼發(fā)送post請求,也可以使用postman來構(gòu)造post請求

使用代碼:

import requests

url = 'https://www.xxxxxx.com/'
# 需要注意的是Content-Length參數(shù)如果沒有,data表單則不會隨著請求被發(fā)送給服務(wù)端,且使用fiddler抓包的過程中,也無法看到data表單
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length':'<calculated when request is sent>'
}
data = {'a': 1, 'b': 2,'c':'測試'}
result = requests.post(url, headers=headers, data=data)
print(result.content.decode('utf-8'))

使用postman

(2)截獲post請求,使用fiddler

(3)接收post請求,返回響應(yīng)

使用django3的版本,目錄結(jié)構(gòu)如下

settings的配置

主路由的配置

from django.contrib import admin
from django.urls import path, re_path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    # 將主路由和子路由綁定
    path('', include('gp_app.urls')),
]

子路由的配置

from django.urls import re_path
from . import views

urlpatterns = [
    # name用于給視圖命名,可以通過reverse反向解析
    re_path(r'try_get/', views.get, name='get請求'),
    re_path(r'try_post', views.post, name='post請求')
]

views.py的配置

from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.
def get(request):
    if request.method == 'get':
        print(request.GET.getlist('c'))
        pass
    return HttpResponse("ok")


def post(request):
    if request.method == 'POST':
        print("request.method:", request.method)
        print("request.POST.getlist('a'):", request.POST.getlist('a'))
        print("request.POST.getlist('b'):", request.POST.getlist('b'))
        print("request.POST.getlist('c'):", request.POST.getlist('c'))
        print("request.POST:", request.POST)
        a = request.POST.get('a', 0)
        c = request.POST.get('c', 0)
        d = str({a: c})  # 需要注意的是,如果要使用HttpResponse來返回響應(yīng),參數(shù)需要是字符串,d如果不轉(zhuǎn)換成str,返回的結(jié)果就是1
        return HttpResponse(d)

運(yùn)行django之后,控制臺的結(jié)果

(4)data表單使用嵌套的數(shù)據(jù)結(jié)構(gòu)如何處理

情況1:使用json_dumps

postData = {
					 'tid': 1'', 
                      'data': [{'name': 'rqlx', 'value': '0', 'sword': 'attr'},{'name': 'rq1', 'value': '1', 'sword': 'attr'}], 
                      'bindParam': 'true'
                      }
# 注意json.dumps轉(zhuǎn)換成json格式,或許也能寫成
# data = json.dumps({'postData':{ 'tid': 1'', 'data': [{'name': 'rqlx', 'value': '0'},{'name': 'rq1', 'value': '1'}], 'bindParam': 'true'}})
# 將json.dumps放在外層
data = {
				'postData':json.dumps(postData)
           }
resp = requests.post(
   url=url,
   data=data,
   headers=headers,
   # cookies=dict_cookie,  # cookie也可以用字典的形式寫到headers,類似于'Cookie':'xxxxxxxxxx'
   timeout=240,
)

情況2:使用url編碼

有的時候表單提交時,需要先進(jìn)行url轉(zhuǎn)碼,關(guān)鍵在于后端到底要接收什么類型的數(shù)據(jù),如果我們不知道后端能處理的數(shù)據(jù),有時就只能靠猜,用不同的方法嘗試將表單處理成能被后端解析的格式

from urllib.parse import urlencode
data = {‘a(chǎn)': 1, ‘b': 2,‘c':‘測試'}
data =urlencode(data)
resp = reuquest.post(url=url,headers=headers,data=data)

2.multipart/form-data

  • multipart/form-data是常用來上傳文件的表單
  • application/json
  • text/xml

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論