-2

Let's say I have a registration page with a user where they create a new account. If the user is already registered by that email, I'll return a 409 CONFLICT status. However, I'm not quite sure what the proper content is required to return with that -- for example, do I return that user object? Do I return nothing? Do I return a text message? Here is what I have thus far:

@app.post("/users", status_code=status.HTTP_201_CREATED)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):

    # ... some logic
    try:
        db.commit()
    except sqlalchemy.exc.IntegrityError:
        return Response(status_code=status.HTTP_409_CONFLICT)

    # ... 

Are there any reference APIs that show what is supposed to be returned with a 409 ?

David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

1

According to the HTTP specification:

This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict.

See this question for a discussion what code should be used and what should be returned.

Paweł Rubin
  • 2,030
  • 1
  • 14
  • 25
  • thanks, are there any example APIs you can point to that would show a request that could be emulated here that would return a 409? Such as Github or StackOverflow or whatever where you try and create a duplicate user account? – David542 Jul 27 '22 at 20:34