二、接口請求構(gòu)造

請求目標(biāo)構(gòu)造

import requests
requests.get("http://httpbin.testing-studio.com/get")

請求參數(shù)構(gòu)造

  • get query:path、 query
  • post body:
    • form:
    • 結(jié)構(gòu)化請求:json、xml、 json rpc
    • binary

Get Query請求

import requests


class TestDemo:
    def test_query(self):
        payload = {
            "level": 1,
            "name": "insane"
        }
        r = requests.get("http://httpbin.testing-studio.com/get", params=payload)
        print(r.text)
        assert r.status_code == 200
  • 運(yùn)行結(jié)果
test_requests.py::TestDemo::test_query 

============================== 1 passed in 0.75s ==============================

Process finished with exit code 0
PASSED                            [100%]{
  "args": {
    "level": "1", 
    "name": "insane"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.testing-studio.com", 
    "User-Agent": "python-requests/2.25.0", 
    "X-Forwarded-Host": "httpbin.testing-studio.com", 
    "X-Scheme": "https"
  }, 
  "origin": "101.205.253.7", 
  "url": "https://httpbin.testing-studio.com/get?level=1&name=insane"
}

Form請求參數(shù)構(gòu)造

import requests


class TestDemo:
    def test_post_form(self):
        payload = {
            "username": "Insane",
            "password": "loafer"
        }
        r = requests.post("http://httpbin.testing-studio.com/post", data=payload)
        print(r.text)
        assert r.status_code == 200
  • 測試結(jié)果
test_requests.py::TestDemo::test_post_form 

============================== 1 passed in 0.74s ==============================

Process finished with exit code 0
PASSED                        [100%]{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "password": "loafer", 
    "username": "Insane"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "31", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.testing-studio.com", 
    "User-Agent": "python-requests/2.25.0", 
    "X-Forwarded-Host": "httpbin.testing-studio.com", 
    "X-Scheme": "https"
  }, 
  "json": null, 
  "origin": "101.205.253.7", 
  "url": "https://httpbin.testing-studio.com/post"
}

文件上傳

  • files = {'file': open('report.xls', 'rb')}
  • r = requests.post(url, files=files)

header構(gòu)造

  • 普通的header:
    • headers = { 'user-agent': 'my-app/0.0.1'}
    • r = requests.get(url, headers=headers)
  • cookie
    • cookies = dict(cookies _are='working')
    • r = requests.get(url, cookies=cookies)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容