问题描述
我试图使用烧瓶测试客户端在我的Flask应用程序中测试一个PUT请求。一切看起来不错,但我不断得到400 BAD的要求。
我使用POSTMAN尝试了相同的请求,并且得到了回应。
这里是代码
from flask import Flask
app = Flask(__ name__)
data = {filename:/ Users
header = {'content-type':'application / json'}
api =http:// localhost:5000 / ingest
with app .test_client()作为客户:
api_response = client.put(api,data = data,headers = headers)
print(api_response)
$ b 输出
响应流式传输[400 BAD REQUEST]
您确实需要对数据进行实际编码到JSON:
导入json
以app.test_client()作为客户端:
api_response = client.put(api,data = json.dumps(data),headers = headers)
将 data
设置为一个字典可以将a一个常规的表单请求,所以每个键值对都将被编码到 application / x-www-form-urlencoded
或 multipart / form-data
内容,如果您使用了任何内容类型。实际上,您的数据完全被忽略。
Im trying to test a PUT request in my Flask app, using flasks test client.Everything looks good to me but i keep getting 400 BAD request.
I tried the same request using POSTMAN and I get the response back.
Here is the code
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)
Output
Response streamed [400 BAD REQUEST]
You do need to actually encode the data to JSON:
import json
with app.test_client() as client:
api_response = client.put(api, data=json.dumps(data), headers=headers)
Setting data
to a dictionary treats that as a regular form request, so each key-value pair would be encoded into application/x-www-form-urlencoded
or multipart/form-data
content, if you had used either content type. As it is, your data is entirely ignored instead.
这篇关于Flask使用自定义标题测试放置请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!