简体   繁体   中英

REST API response status code when a related object fails to save

I have a rest API that acts like a wrapper for a couple of other external APIs. Saving a local session object plus two objects based on the responses of those APIs.

A successful call will result in something like this:

{
    session {
        session_id: 123,
        ...
    },
    data1 {
        id: 456
        ...
    }
    data2 {
        id: 789
        ...
    }
}

In the event of one of those APIs failing or being unavailable I was planning on replacing the content of the block with just an error message like this:

{
    session {
        session_id: 123,
        ...
    },
    data1 {
        error: "service unavailable"
    }
    data2 {
        id: 789
        ...
    }
}

In that situation would a 201 status code still be the most appropriate as while the session object was successfully created the related data was not.

202 status code is used when your API is acknowledging that you have received the request and the requestor doesn't expect an immediate response(async). You can notify the requestor(could be via a callback) regarding on the status of the said request.

If you're doing a synchronous request, you should respond with the appropriate error code, instead of a 202 response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM