I am using django v2.2.4
and need to access request body data.
Here's my code:
@api_view(['POST'])
@renderer_classes((JSONRenderer,))
def index(request):
if request.method == 'POST':
results= []
data = JSONParser().parse(request)
serializer = ScrapeSerializer(data=data)
if serializer.is_valid():
url = request.data.url
#url = request.POST.get('url')
But I get this error:
RawPostDataException at /scrape/
You cannot access body after reading from request's data stream
Here's the request body:
{
"url": "xyz.com"
}
How can I access the request body?
I have found this SO post that related to this issue, Exception: You cannot access body after reading from request's data stream
Anyway use request.data
instead of request.body
in DRF
@api_view(['POST'])
@renderer_classes((JSONRenderer,))
def index(request):
if request.method == 'POST':
results = []
if serializer.is_valid():
The request.data
returns the parsed content of the request body and it will be dict like object , hence the dot operation ( request.data.url
) won't works here.
要访问POST请求的请求正文,您可以通过url = request.POST.get("url")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.