Stack Exchange
Axios Error: request failed with status code 400
Tried Axios to communicate with the API. A kept getting Axios Error: request failed with status code 400.
import React, { useState } from 'react';
import { View, Button } from 'react-native';
import axios from 'axios';

const LoginScreen = () => {
  const [token, setToken] = useState('');

  const handleLogin = async () => {
    try {
      const response = await axios.post(
        'http://ec2-3-94-8-1.compute-1.amazonaws.com:8080/api/auth/v1/login?',
        {
          login: 'login12345',
          senha: 'senha12345'
        },
        {
          headers: {
            'Content-Type': 'application/json'
          }
        }
      );

      const { token } = response.data;
      setToken(token);
      console.log('Received token:', token);
    } catch (error) {
      console.error('Error:', error);
    }
  };

  return (
    <View style={{marginTop: 100}}>
      <Button title="Login" onPress={handleLogin} />
    </View>
  );
};

export default LoginScreen;

I was able to fix the error by putting the login and password object's strings into the URL.
const response = await axios.post(
        'http://ec2-3-94-8-1.compute-1.amazonaws.com:8080/api/auth/v1/login?login=login12345&senha=senha12345',
        {
          headers: {
            'Content-Type': 'application/json'
          }
        }
      );

First, the other posts I saw about that error suggest that I was trying to use the const { token } = response.data; while it was still pending. Adding const { token } = await response.data; did not change anything. I was able to avoid the error without changing the it.
Second, it also means I had to put sensitive information(the false password and login examples given) on the URL. Is there any other way I can fix it without having to pass the login information like this?
