我正在为我的REST API编写一些测试,并且在POST请求方面有些挣扎
我有要发送的数据:
RECID | DATE | STARTED
------+----------+--------------------
COB | 20190122 | 02/04/2019 22:06:37
COB | 20190121 | 02/04/2019 22:02:41
我已经按照以下方式构建了标头和POST请求:
data = {
"patient": 34,
"measure_cat": 52,
"measure_type": "1",
"date": "2019-06-11",
"value": '',
"unit": '',
"nnu_value": "3"
}
测试是一个简单的201断言:
headers = {'Authorization': 'Token '+ self.token.key, 'content_type':'application/json', 'Accept-Language' : 'en'}
response = requests.post('http://127.0.0.1:8000/saveMeasure/', json.dumps(data), headers=headers)
服务器以HTTP状态415 Unsupported Media Type回答:
self.assertEqual(response.status_code, 201)
我错过了配置步骤或其他什么吗?
我不知道它是否相关,但是我正在使用灯具作为测试数据库。 这就是我所有的进口商品:
Unsupported Media Type: /saveMeasure/
[21/Jun/2019 09:29:47] "POST /saveMeasure/ HTTP/1.1" 415 62
更新:
这是我发布请求的视图代码:
import json
import requests
from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory, APITestCase, APIClient
这是测试的一部分:
def post(self, request, *args, **kwargs):
# remove persistant data
persistant = False
newMeasures = []
for measure in request.data:
persistant = check_persistant_measure(measure['measure_cat'], measure['measure_type'], measure['patient'], measure['value'], measure['nnu_value'], measure['b_value'])
if persistant == False:
newMeasures.append(measure)
# add Related measure automatically calculated to request data
try:
calc_data = get_related_measures(newMeasures)
for newM in calc_data:
newMeasures.append(newM)
except:
pass
serializer = self.get_serializer(data=newMeasures, many = True, allow_empty = False)
serializer.is_valid(raise_exception = True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)