Back

Internal curl error with cloud function

  • 0
  • Functions
sazzike
31 May, 2023, 02:45

The client is setup with

TypeScript
  client.setEndpoint('https://cloud.appwrite.io/v1')

but I haven't set an endpoint for the server, I thought this was automatically set (I'm using Appwrite cloud)

TL;DR
The user is experiencing an internal curl error with their cloud function. They have tried to execute the function from the console, but it returns an error status. They plan to refactor the function to improve error handling but need to leave for work. Suggestions are given to wrap the code in a try-catch block and convert 'then' to 'await'. They are advised to ensure that 'res.json' is called exactly once. It is recommended to validate the JSON payload by wrapping the keys in double quotes. The user is informed that the JSON provided in their sample is not valid. The difference between 'APPWRITE_FUNCTION_DATA' and 'req
Binyamin
31 May, 2023, 02:46

It's okay, it can be confused at start In your function settings page you should set this values APPWRITE_FUNCTION_ENDPOINT APPWRITE_FUNCTION_PROJECT_ID APPWRITE_FUNCTION_API_KEY

sazzike
31 May, 2023, 02:48

Ah ok, no mine are empty. If I set these, won't they be overwritten each time I deploy the function from the CLI?

Binyamin
31 May, 2023, 02:48

No

Binyamin
31 May, 2023, 02:48

This part is not related to deploy

sazzike
31 May, 2023, 02:49

Ok great I'll try that, thanks for your help @Binyamin

sazzike
31 May, 2023, 02:50

Sorry one more quick question @Binyamin am I passing data correctly between the client and server using JSON.stringify() and JSON.parse() above?

Binyamin
31 May, 2023, 02:50

Yes, this is the right way to hanlde function data.

sazzike
31 May, 2023, 02:50

Great, thank you

Binyamin
31 May, 2023, 02:51

<:appwriteupvote:899677724615016528>

Drake
31 May, 2023, 02:54

You should be using req.payload for the input data rather than the event data variable. The event data variable is for executions triggered by events

sazzike
31 May, 2023, 02:56

Thanks @Steven I'll change to

TypeScript
  const payload = JSON.parse(req.payload)
Binyamin
31 May, 2023, 02:58

What is the different between APPWRITE_FUNCTION_DATA and req.payload It looks like they both holds the same information don't they?

https://github.com/appwrite/appwrite/blob/master/app/controllers/api/functions.php#L1152 https://github.com/appwrite/appwrite/blob/master/app/executor.php#L507

sazzike
31 May, 2023, 03:07

I made the changes above, deployed and got the same error. Then I tried the 'Execute Now' button in the console with some mock JSON data, and got a different error:

TypeScript
SyntaxError: Unexpected token t in JSON at position 5
    at JSON.parse (<anonymous>)
    at module.exports (/usr/code-start/src/index.js:21:24)
    at /usr/local/src/server.js:68:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
TypeScript
req {"variables":{"APPWRITE_FUNCTION_ENDPOINT":"https://cloud.appwrite.io/v1","APPWRITE_FUNCTION_PROJECT_ID":"removedforprivacy","APPWRITE_FUNCTION_API_KEY":"removedforprivacy","APPWRITE_FUNCTION_ID":"647687110d37ce2558c1","APPWRITE_FUNCTION_NAME":"setupTeam","APPWRITE_FUNCTION_DEPLOYMENT":"6476b848e8e486567ea9","APPWRITE_FUNCTION_TRIGGER":"http","APPWRITE_FUNCTION_RUNTIME_NAME":"Node.js","APPWRITE_FUNCTION_RUNTIME_VERSION":"16.0","APPWRITE_FUNCTION_EVENT":"","APPWRITE_FUNCTION_EVENT_DATA":"","APPWRITE_FUNCTION_DATA":"{\n   teamId: \"123d3984ur\", \n    teamName:\"Demo Team\"\n}","APPWRITE_FUNCTION_USER_ID":"6475dab3e1dd9253ae62","APPWRITE_FUNCTION_JWT":"removedforprivacy","INERNAL_EXECUTOR_HOSTNAME":"exc1"},"headers":{},"payload":"{\n   teamId: \"123d3984ur\", \n    teamName:\"Demo Team\"\n}"}
res {}
Drake
31 May, 2023, 03:08

req.payload is when the create execution API is called. Event data is for executions triggered by events: https://github.com/appwrite/appwrite/blob/bbc9bb823df1a4afb5d83baf96a1a25e57d279a5/app/workers/functions.php#L299

Drake
31 May, 2023, 03:09

Your mock data is not valid JSON

sazzike
31 May, 2023, 03:09

Ok, I carefully changed the keys/values on the demo data, but I will try again and validate the JSON this time

Drake
31 May, 2023, 03:13

JSON keys should be wrapped with double quotes

sazzike
31 May, 2023, 03:13

The JSON provided in the sample is not valid. I tried again with valid JSON and back to the internal curl error

Binyamin
31 May, 2023, 03:13

Not in both? It's seems like req.payload is just a placeholder for $data

Drake
31 May, 2023, 03:14

I'm pretty sure this was fixed in the latest version of Appwrite

sazzike
31 May, 2023, 03:15

I'm using Cloud so not sure about versions. Anyway the internal curl error is thrown with valid JSON payload, and all the variables updated, so not sure what it can be. I wish we could get a better error message to narrow it down.

Binyamin
31 May, 2023, 03:16

If you try to execute in this way Try this

TypeScript
{"teamId":"123d3984ur","teamName":"Demo Team"}

Or this

TypeScript
{teamId:"123d3984ur",teamName:"Demo Team"}
Drake
31 May, 2023, 03:19

Try wrapping all your code in a try catch and make sure res.json is called exactly once

To make things simpler, you should also convert the then to await

sazzike
31 May, 2023, 03:21

Ok thanks @Steven and @Binyamin . The databases are being created when I execute from the console, but the execution status is error. I'll refactor the function to catch better errors, but for now I have to go to work. Thanks for your help so far.

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