title: FastAPI: React native frontend giving 422 error during auth flow
body: <p>My react native code:</p>
  <pre><code>const signin =
    (dispatch) =&gt;
    async ({ username, password }) =&gt; {
      try {
        console.log(username, password);
        const response = await tracker.post(
          &quot;/login&quot;,
          (data = { username, password }),
          (headers = {
            &quot;content-type&quot;: &quot;application/x-www-form-urlencoded&quot;,
          })
        );
        await AsyncStorage.setItem(&quot;token&quot;, response.data.token);
        dispatch({ type: &quot;signin&quot;, payload: response.data.token });
        console.log(response.data.token);
      } catch (err) {
        console.log(err);
        dispatch({
          type: &quot;error&quot;,
          payload: &quot;This is an error, start debugging&quot;,
        });
      }
    };
  </code></pre>
  <p>Curl request to FastAPI backend:</p>
  <pre><code>curl -X 'POST' \  'https://fastest.herokuapp.com/login/' \   -H 'accept: application/json' \   -H 'Content-Type: application/x-www-form-urlencoded' \   -d 'grant_type=&amp;username={email}&amp;password={password}&amp;scope=&amp;client_id=&amp;client_secret=
  </code></pre>
  <p>whenever I try to create a new user or sign in with an existing user I keep getting following error:</p>
  <pre><code>[AxiosError: Request failed with status code 422]
  </code></pre>
  <p>Is there a better way to send a POST request with curl to signup or login using axios?</p>
  <p>Now, I know this is a well documented error on internet, but, somehow, I am unable to find the error and debug it. Any feedback as to what I am doing wrong?</p>
  <p>Edit:
  FastAPI endpoint code:</p>
  <pre><code>@router.post(&quot;/&quot;,response_model=schemas.Token)
  def getLogin(user_Credentials:OAuth2PasswordRequestForm=Depends(),db: Session=Depends(database.get_db)):
      user = db.query(models.User).filter(models.User.email == user_Credentials.username).first()
      if not user:
          raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f&quot;wrong credentials&quot;)
  
      if not utils.verify(user_Credentials.password,user.password):
          raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f&quot;wrong credentials&quot;)
  
      access_token = oauth2.create_access_token(data={&quot;user_id&quot;: user.id})
      return {&quot;access_token&quot;:access_token, &quot;token_type&quot;:&quot;bearer&quot;}
  </code></pre>
  <h5>For full code:</h5>
  <p>Backend FastAPI: <a href="https://github.com/bhavyasharma95/FastAPI-quickstart" rel="nofollow noreferrer">here</a>
  Frontend react native: <a href="https://github.com/bhavyasharma95/Reactnative-Quickstart" rel="nofollow noreferrer">here</a></p>
  <p><strong><strong><strong><strong><strong><strong><strong><strong>Error Changed</strong></strong></strong></strong></strong></strong></strong></strong></p>
  <p>After adding <code>qs.stringify()</code> according to <a href="https://axios-http.com/docs/urlencoded" rel="nofollow noreferrer">https://axios-http.com/docs/urlencoded</a> and updating the code as follows:</p>
  <pre><code>const signin =
    (dispatch) =&gt;
    async ({ username, password }) =&gt; {
      try {
        console.log(username, password);
        const response = await tracker({
          method: &quot;post&quot;,
          url: &quot;/login&quot;,
          data: qs.stringify({
            username: username,
            password: password,
          }),
          headers: {
            &quot;content-type&quot;: &quot;application/x-www-form-urlencoded;charset=utf-8&quot;,
          },
        });
        console.log(response.data.token);
        await AsyncStorage.setItem(&quot;token&quot;, response.data.token);
        dispatch({ type: &quot;signin&quot;, payload: response.data.token });
      } catch (err) {
        console.log(err);
        dispatch({
          type: &quot;error&quot;,
          payload: &quot;Start debuggin&quot;,
        });
      }
    };
  </code></pre>
  <p>the problem arises now is that token is <code>undefined</code>, but the when I enter same credentials on <code>/docs</code> I get the token.</p>
  <p>Final update: got the endpoint wrong for token access</p>