0

Application stack: Django React

I am trying to extract email and password from the request sent from react.

Instead of getting email and password, I am getting None value.

The data sent is in FormData type.

This is the function handling signin in the backend(only required part):

def signin(request):
    if not request.method == 'POST':
        return JsonResponse({'error': 'Send a post request with valid paramenter only'})

# username = request.POST['email']
# password = request.POST['password']

username = request.GET.get('email')
password = request.GET.get('password')

print(username)  #printing None
print(password)  #printing None

These are the front end codes:

Here is the function handling form submission:

const onSubmit = (event) => {
        //console.log({email,password})
        event.preventDefault();
        setValues({...values, error:false, loading:true})
        signin({email, password})
            .then(data => {
            console.log("DATA", data);   
            if (data.token){
                let sessionToken = data.token;
                authenticate(sessionToken, () => {
                    console.log("TOKEN ADDED");
                });
            }})
            .catch(e => console.log(e));
    };

This is signin function:

export const signin = (user) => {
    const formData = new FormData();
    for(const name in user){
        formData.append(name, user[name]);
    }
    return fetch(`${API}user/login/`,{
        method:"POST",
        body:formData    //JSON.stringify(user)
    })
    .then((response) => {
        console.log("SUCCESS", response);
        return response.json();
    })
    .catch((err) => console.log(err));
};

This is authenticate function:

export const authenticate = (data,next) => {
    if(typeof window !== undefined){
        localStorage.setItem("jwt", JSON.stringify(data));
        next();
    }
};

I tested it on postman also, same error is coming. Can anyone help me how to grab the email and password from the request.

  • print **None** because it doesn't find the key(email, password) in the **request**. Check in your **formData** the keys and values are added – Erick Cruz Nov 18 '20 at 19:21
  • Email and password are added, you can see in the signin and onSubmit functions of JS but in the backend I am not able to extract them. – Nadeem Ahmed Nov 19 '20 at 05:30

0 Answers0