Fastapi: Can't view JSON response error from client

Created on 3 Dec 2020  路  4Comments  路  Source: tiangolo/fastapi

I have an endpoint for creating a user. I've tested this endpoint and it is working as expected.

When I make an invalid request from PostMan, I get the expected response:

{
    "detail": [
        {
            "loc": [
                "body",
                "email"
            ],
            "msg": "value is not a valid email address",
            "type": "value_error.email"
        }
    ]
}

Say I make an invalid request from client (in this case I am using axios and vue.js)

Here is the request from the frontend (edited for readability)

async login() {
      try {
        const data = await axios.post(
          process.env.VUE_APP_API + "/users/open",
          this.user
        )
        console.log(data);
       //handle
        }
      } catch (error) {
        console.log(error);
      }
    }
  }

When I make this same exact request, all I see in my console is
POST http://localhost/api/v1/users/open 422 (Unprocessable Entity)
Register.vue?0103:137 Error: Request failed with status code 422
at createError (createError.js?2d83:16)
at settle (settle.js?467f:17)
at XMLHttpRequest.handleLoad (xhr.js?b50d:69)

Why can't I see the response that postman can see? I've compared the HTTP request to the best of my ability, and they seem the same in aspects that I believe are important. (content-type is application/json)

I have this same issue when I raise exceptions. It returns the same error code, not the actual code I raised.

Any thoughts? Thanks so much - I love this library so if you are a contributor reading this, extra thanks :)

question

All 4 comments

At first look, your field email inside the body is invalid... And that's it. :thinking:
Are you sure this.user has a valid email?

If you could provide a sample request body you're using, and maybe the endpoint function, it could be helpful.

That response is expected. If I put a valid email it gives me a response code of 200 and creates the user. I know the endpoint works correctly. My issue is that I cannot see the "error" response when I purposefully pass an object with an invalid email in my frontend. I can only see it in Postman.

If you'd like to see the endpoint, I could paste it here. It is the exact endpoint generated for a project from this repo:
https://github.com/tiangolo/full-stack-fastapi-postgresql

I'm making a POST at http://localhost/api/v1/users/open
with this body in Postman
{
"email": "lucas@",
"password": "lu"
}

I really don't think it is a problem with the endpoint though. i think it is something with the way my client code is doing it.

If you need the endpoint code let me know and I'll paste it all in. It's a bit lengthy

But then it's not a FastAPI issue.

Try to console.log(error.response.data): https://stackoverflow.com/questions/45017822/catching-error-body-using-axios-post

Please next time try to follow the template, it helps us on the response time. If you can close the issue, it will be highly appreciated.

Besides, some non asked additional tip:

  • If you use three single quotes + programming language name, you have highlighted snippets. :sunglasses:
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return "Hello World!"
Was this page helpful?
0 / 5 - 0 ratings