Back

What is status code property name for API responses?

  • 0
  • General
Matej
20 Jul, 2023, 22:51

Or, let me rephrase the question If only a User Object is returned from this. how do I pick response status code?

TL;DR
The user is asking about the status code property name for API responses and its practical importance. They want to set and return custom messages to the client side based on the status code. The user provides a code example using axios to handle different status codes. They question the need for status codes and provide examples of custom messages they want to display to the user. The support person explains that the SDK abstracts the status code and only returns the data on success, while throwing an exception on failure. The user questions why the status code property is left out from success and requests its addition. The support person clarifies that the SDK only returns the body
Drake
21 Jul, 2023, 00:10

you can't. The SDK will only return the body

Failed API calls will throw an exception. The AppwriteException has a code which is the status code

Matej
21 Jul, 2023, 00:12

Is there also a range of 200 codes?

Status code on success would be a nice addition for 2xx checks

Matej
21 Jul, 2023, 00:15

Is there any good reason why status code prop is left out from success? Isn't it usually a common practice to have them?

Drake
21 Jul, 2023, 00:16

the SDK abstracts that away to make things easier. if the API call is successful, you get the data back. If it's not successful, an exception is thrown

Matej
21 Jul, 2023, 00:17

But limits us a little bit from making custom responses based on status codes as we can't pick them all right now

Drake
21 Jul, 2023, 00:17

what is that? why exactly do you need the status code? what are you trying to do?

Matej
21 Jul, 2023, 00:19

For example, if some API would return 200, I can make custom message for that one and return it to the client side to display it to the user. Same with 201 and other 2xx code.

Drake
21 Jul, 2023, 00:21

what custom message? do you have a more concrete example or detail?

Matej
21 Jul, 2023, 00:24

Whatever you'd like to say. For example, if https status code is 201, then I can return a message like "Your account is created"...if 202 - "We are processing your request"... While you probably have a range of what http status codes you return on failure, it would be nice to have it on success as well

Drake
21 Jul, 2023, 00:25

do you have actual code sample? because i'm not really understanding the need

Drake
21 Jul, 2023, 00:27

you make an api call:

TypeScript
try {
  final document = databases.createDocument();
  print("your document is created");
} catch (e) {
  print("failed to create your document");
}
Drake
21 Jul, 2023, 00:27

i don't need the HTTP status code

Matej
21 Jul, 2023, 00:31
TypeScript
const response = {
  message: '',
  statusCode: 400
}
try {
const r = await axios.get(url);
if (r.statusCode === 200) {
  response.message = 'Your request was successfull';
  response.statusCode = r.statusCode;
} else if (r.statuscode === 201) {
    response.message = 'Your request was created';
    response.statusCode = r.statusCode;
} . . . . . . 

} catch (e) {
   . . . 
}

return response;
Matej
21 Jul, 2023, 00:31

But I'd make switch function for it

Matej
21 Jul, 2023, 00:32

And with this I can apply custom messages per cloud function, or make more general ones...

Drake
21 Jul, 2023, 00:35

per cloud function? Are you referring to Appwrite Functions?

Matej
21 Jul, 2023, 00:35

Yes

Matej
21 Jul, 2023, 00:35

But doesn't need to apply just to functions, can apply elsewhere as well

Drake
21 Jul, 2023, 00:37

sorry i still don't see the value in what you're trying to do...

Matej
21 Jul, 2023, 11:27

🀷

Often clients (and servers too) are using status codes to do stuff based on what the code is.

Matej
21 Jul, 2023, 11:27

Especially with Restful APIs

Drake
21 Jul, 2023, 14:30

Ya I understand what status codes are and what they mean. But I don't understand their practical importance in how you're using the SDK in your app

Matej
21 Jul, 2023, 14:31

Well, my current use case (for now) is to set and return custom messages to the client side. Another use case might be setting specific component or global states on the client side based on the certain HTTP status code - to which I didn't get yet code-wise, in the current project.

Drake
21 Jul, 2023, 14:33

You can already show custom messages

TypeScript
try {
  final document = databases.createDocument();
  print("your document is created");
} catch (e) {
  print("failed to create your document");
}

If you're calling create document, and it's successful, a document was created

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more