I am trying to send some data from my React frontend to my Django REST backend.
My Django application does NOT have models.
I have a react form. When the user submit the button, the form "submits" three params:
firmNAme
timeframes
numberOfResults
I want to send these three parameters to my Django backend. To do so, I m assuming React is sending a "POST" request to the Django endpoint. I need these params in Django so that I can use them to do some manipulations for a ML model.
My React function
handleSubmit(event){
event.preventDefault()
fetch("/myapi/getreactinfotwitter",{
method:"POST",
headers:{
'Accept':'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
'data_options': {
'partnerName': this.state.firmName,
'timeframes': this.state.timeframes,
'numberOfResults': this.state.numberOfResults,
}
})
})
Now, I think the the Reast "POST" has to be matched with a Django "GET", so that Django gets the three params.
In DJANGO I have:
urls.py
path('getreactinfotwitter/', getreactinfotwitter, name="getreactinfotwitter")
myapi/views.py
@api_view(['GET', 'POST'])
def getreactinfotwitter(request):
print(request)
if request.method == 'GET':
return Response(request.data)
HOWEVER
My
requestin theviews.pyis not getting anything.In the web console, when I click submit I get
POST http://localhost:8080/myapi/getreactinfotwitter/ 500 (Internal Server Error)
postmethod then, usepostalso inside the django view. I would recommend you to use the Postman software to check first your django-rest api works and then work with the Reactpost,get,put,patch,deleteetc are http methods by which client requests server to do something. At this API era, the same API url is used for different purposes (to get some data from server, to save some data to the database by the backend code, to edit something etc.). That's why the server has to check the method of request to response client. If client needs something from server then client send agetrequest to the server via an url. The server then check the method of request (here,get) and responses something depending on the method.