Skip to content
Back

500 from a function

  • 0
  • Flutter
  • Functions
rosario
25 Mar, 2024, 09:22

Hi guys, what am I doing wrong? I am sending the postman request to my function. The header contains only a content-type = application/json; charset=UTF-8. From this request and receive a 500 internal server error.

Here is the function:

String requestBody = jsonEncode(context.req.body); context.log(requestBody); String userId = context.req.body['user_id']; context.log(context.req.body['text']); if (await userExists(userId)) { List? response = await getStatusCode(context.req.body['text']); return context.res.json({'status': response![0], 'text': response[1]}); } else { return context.res.json({'status': 400, 'text': 'User does not exist'}); }

The requestBody variable is an empty string "". Why?

TL;DR
Developers are getting a 500 internal server error when sending a request to their function with an empty request body. The issue might be due to the ChatGPT API taking too long to respond, causing an execution timeout. They're also not setting the request body correctly in their code. They need to ensure the body contains the necessary data and check the execute permissions for the function.
Ernest
25 Mar, 2024, 09:33

Log and check the execution logs if response[1] does have non-empty text

Ernest
25 Mar, 2024, 09:34

Also have you set the right execute permissions for function?

rosario
25 Mar, 2024, 10:29

The permission is set on Any.

So I could solve the problem with Postman. But I get the error "Execution timed out.". I suppose this is because the function call the ChatGPT API which takes times to return an answer. How can I solve this issue?

The other issue occurs if I send the request to the function from my code:

TypeScript
final response = await http.post(
  Uri.parse('https://65fd719a7c3c7d2c8e8c.appwrite.global'),
  headers: <String, String>{
    'Content-Type': 'application/json; charset=UTF-8',
    'Connection': 'keep-alive',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept': '*/*',
    'Host': '65fd719a7c3c7d2c8e8c.appwrite.global',
  },
  body: jsonEncode(<String, String>{
    'user_id': userId,
    'text': text,
  }),
);

In this case, the request body is an empty string. This should not be empty.

rosario
25 Mar, 2024, 10:59

The log of the request from the code shows that the HTTP method is an OPTIONS.

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