2

I am using django-rest-framework at backend and I am trying to post following data from react frontend.

handleSubmit() {
   fetch('http://127.0.0.1:8000/api/debt/create',{
       method: 'POST',
       headers: {
           Authorization: `Bearer ${localStorage.getItem('id_token')}`,
           'Content-Type': 'application/json'
       },
       body: JSON.stringify({
           user: '1',
           customer: '9',
           debtKey: 'b1d9ef1b-45ad-4dc5-80cc-95f7994c0aae',
           createduserKey: '0245abb9-2837-4f37-ae02-9be1b88887ef',
           totalDebt: 789,
           receivedAmount: 115,
           description: 'Debt description',
           paymentDate: '01.01.2019'
       }),
   }).then(response => {
       console.log(response);
       return response.json()
   }).then(json => {
       //this.setState({
       //    user:json
       //});
   });
 console.log("Form Sumbmssion");
}

But I am getting "BAD REQUEST" error. What I am missing? Thanks

1
  • 2
    A bad request means the data you are sending from React to Django is not what Django expects. Try to log any validation errors in Django to see which post data is missing or incorrect. It could be something as simple as a missing field or invalid data. You need to post your Django view and possibly models to work it out. Commented Mar 30, 2021 at 8:56

2 Answers 2

2

I think you just need to add "/" to http://127.0.0.1:8000/api/debt/create at the end. Bad request is often due to that issue only. But why are you using fetch anyway. Please consider using axios if you are using React.js on the frontend. It is identical to fetchAPI, but is lot more flexible.

Sign up to request clarification or add additional context in comments.

Comments

1

Authorization is a key .it should be in quotes.

headers: {
       'Authorization': `Bearer ${localStorage.getItem('id_token')}`,
       'Content-Type': 'application/json'
   },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.