0

I made a post request to this url http://localhost:8000/api/auth/users/ on postman . It was successful. However, trying the same thing on my react front end produces a 400 (bad request error). It also says Uncaught (in promise) Error: Request failed with status code 400

Here's my code AuthAPi.js:

import API from "./API"; 

const config = {
  headers :{
    "Content-Type": "application/json",
   }
}

const signIn = (email, password) => API.post("/auth/users/", {
  params : {
    email : email,
    password : password 
    
  },
},
  config
)
.Catch((error) => console.log( error.message ) );

export { signIn };

and API.js

import axios from 'axios';

export default axios.create({
  baseURL: `http://127.0.0.1:8000/api`,
  headers: {
    "Content-type": "application/json"
  }
});

I've tried several links without solutions. I'd be glad to get assistance here.

There is below a screenshot a console log, Network log and Django server log:

A Console log

Network part 1

Network part 2

Network part 3

Log of Django server

2
  • Could you share your Django code? The bad request (400) error is not due to CORS policy. Commented Nov 10, 2020 at 12:54
  • Which part or file of the Django code ? Commented Nov 10, 2020 at 15:00

2 Answers 2

1

With this code:

API.post("/auth/users/", {
  params : {
    email : email,
    password : password 
  }
}

You’re passing an object which contains an object to axios as post data. Axios doesn’t mind, but your server no-likey. Instead, try:

API.post("/auth/users/", {
  email : email,
  password : password 
}, config)

This should work. If you want to pass in as a single object, you need to declare it outside of the api call:

let postData = {
  email: email,
  password: password
}
API.post("/auth/users/", postData, config)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I tried it this way and it still doesn't work.
Yes, it still the same
We need to see the response and request objects. You’ll find them in the network tab in your dev tools. Update your question with screenshots or copy the entire text (headers and body data). That way we’ll be able to troubleshoot a little further.
Thanks, I have just added images @Nick Dawes
1

params are the URL parameters to be sent with the request

The signature of post instance method that you used is axios#post(url[, data[, config]]), thus you should make your request like this

const signIn = (email, password) => API.post("/auth/users/", {
    email : email,
    password : password 
  },
  config
)

1 Comment

Thanks, I tried it this way and it still doesn't work.

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.