Back

Error Executing Appwrite Function via POST Request

  • 0
  • Self Hosted
  • Flutter
  • Functions
ZiaChoudhary
29 May, 2024, 15:23

Last day I deployed a function to my selfhosted Appwrite and its working fine when I execute it from console.

It even worked fine when I execute a GET request from my Flutter Windows App.

But It is showing following error when a POST request executed from Flutter App:

TypeScript
  File "/usr/local/server/src/server.py", line 210, in handler
    return await action(request)
  File "/usr/local/server/src/server.py", line 131, in action
    context.req.body = request.get_json(force=True, silent=False)
  File "/usr/local/server/src/function/runtime-env/lib/python3.9/site-packages/werkzeug/wrappers/request.py", line 623, in get_json
    rv = self.on_json_loading_failed(e)
  File "/usr/local/server/src/function/runtime-env/lib/python3.9/site-packages/flask/wrappers.py", line 131, in on_json_loading_failed
    raise BadRequest()
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.```

My Flutter Code:

```Execution result = await _functions.createExecution(
      functionId: AppwriteCredentials.hookGeneratorFunctionId,
      body: "{'user_message': $userInputText}", // optional
      method: ExecutionMethod.pOST, // optional
      headers: {"Content-Type": "application/json"}, // optional
    );```
TL;DR
Issue with POST request from Flutter app to self-hosted Appwrite server. The provided JSON body format is invalid due to the use of single quotes. POST method is case-sensitive, but not the root cause of the problem. The error seems related to decoding JSON response on the server side. The Flutter code should use double quotes for JSON keys and values.
ideclon
29 May, 2024, 15:33

Is it possible POST is case sensitive? And are you handling POST requests in your Function?

ZiaChoudhary
29 May, 2024, 15:38

@ideclon As I said its working properly when executed from console.

The only problem is with POST request from flutter.

ideclon
29 May, 2024, 15:43

Is it possible POST is case sensitive?

ideclon
29 May, 2024, 15:43

(I don’t know Flutter, but that doesn’t seem unreasonable)

ZiaChoudhary
29 May, 2024, 15:45

Is it possible POST is case sensitive? I did not get your point. I think This issue is more related to decoding json response coming to server.

ideclon
29 May, 2024, 15:46

Well, you set the method as ExecutionMethod.pOST

ZiaChoudhary
29 May, 2024, 15:47

ExecutionMethod.pOST It is provided by appwrite SDK for Flutter as its is. and this is not the issue

Steven
29 May, 2024, 15:47

that body you have is invalid JSON. you can't use single quotes

ZiaChoudhary
29 May, 2024, 15:49

EDITED: @Steven I Tried these and non of them worked: 1. body: "{'user_message': $userInputText}",

2. body: '{"user_message": $userInputText}'

TypeScript

    Execution result = await _functions.createExecution(
      functionId: AppwriteCredentials.hookGeneratorFunctionId,
      body: body, // optional
      method: ExecutionMethod.pOST, // optional
      headers: {"Content-Type": "application/json"}, // optional
    );```
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