[英]Flask Testing a put request with custom headers
我試圖使用flasks測試客戶端在我的Flask應用程序中測試PUT請求。 一切對我來說看起來不錯,但我不斷收到400錯誤的要求。
我使用POSTMAN嘗試了相同的請求,並且得到了響應。
這是代碼
from flask import Flask
app = Flask(__name__)
data = {"filename": "/Users/resources/rovi_source_mock.csv"}
headers = {'content-type': 'application/json'}
api = "http://localhost:5000/ingest"
with app.test_client() as client:
api_response = client.put(api, data=data, headers=headers)
print(api_response)
輸出量
Response streamed [400 BAD REQUEST]
您確實需要將數據編碼為JSON:
import json
with app.test_client() as client:
api_response = client.put(api, data=json.dumps(data), headers=headers)
將data
設置為字典會將其視為常規表單請求,因此,如果您使用了兩種內容類型,則每個鍵值對都將被編碼為application/x-www-form-urlencoded
或multipart/form-data
內容。 實際上,您的數據將被完全忽略。
我認為使用json
參數而不是data
參數傳遞數據更簡單:
reponse = test_client.put(
api,
json=data,
)
從這里報價:
在測試客戶端方法中傳遞json參數會將請求數據設置為JSON序列化的對象,並將內容類型設置為application / json。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.