appwrite/sdk-for-web

๐Ÿ› Bug Report: File Upload not working

Gustl22 opened this issue ยท 4 comments

๐Ÿ‘Ÿ Reproduction steps

When I want to create a file using createFile I get the error:

AppwriteException: Network request failed at Appwrite._callee56$ (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:261917:25) at tryCatch (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:28085:19) at Generator.invoke [as _invoke] (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:28255:24) at Generator.throw (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:28127:23) at rejected (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:258615:34) at tryCallOne (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:32938:14) at http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:33039:17 at http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:33479:21 at _callTimer (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:33367:9) at _callImmediatesPass (http://192.168.178.53:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:33406:7)

I debugged the request at the server and I get the POST request:

"POST /v1/storage/buckets/default/files/ HTTP/2.0" 400 1527 "-" "okhttp/3.14.9"

But when I do it with the appwrite web console I get:

"POST /v1/storage/buckets/default/files HTTP/2.0" 201 216 "https://appwrite.oberhauser.dev/console/storage/bucket?id=default&project=619d57cbe54c9" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0"

So it looks like projectID and bucketID is missing.

And indeed, if I add the two params to this line https://github.com/appwrite/sdk-for-web/blob/6bedd589a9be12407d7756dd3231b4459a7f8631/src/sdk.ts#L2935

payload['id'] = bucketId;
payload['project'] = this.config.project;

and also add the query params to POST requests: https://github.com/appwrite/sdk-for-web/blob/6bedd589a9be12407d7756dd3231b4459a7f8631/src/sdk.ts#L1098

then the request is accepted, but returns an empty error message.

Appwrite version is 0.13.3, couldn't select it from this template.

๐Ÿ‘ Expected behavior

It should be able to upload the file, so it should lead to this request + body:

"POST /v1/storage/buckets/default/files HTTP/2.0" 201 216 "https://appwrite.oberhauser.dev/console/storage/bucket?id=default&project=619d57cbe54c9" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0"

๐Ÿ‘Ž Actual Behavior

It leads to this request:

"POST /v1/storage/buckets/default/files/ HTTP/2.0" 400 1527 "-" "okhttp/3.14.9"

๐ŸŽฒ Appwrite version

Different version (specify in environment)

๐Ÿ’ป Operating system

Linux

๐Ÿงฑ Your Environment

Using this in a react-native app and accessing an appwrite server on a Raspberry PI 4, running Ubuntu Server 21.10

๐Ÿ‘€ Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

๐Ÿข Have you read the Code of Conduct?

@Gustl22 The request is fine, the project id is set in the header by SDKs (you need to call .setProject()) in the SDK.
The request is giving 400 error which means some params are missing. Was the file properly provided? Please check the console error docker-compose logs appwrite you will see the log there, also the SDK should throw proper exception, seems this is being used in Expo and something is not right. So please provide us the docker console logs immediately after this failed request.

I don't see where in code the projectId is set for the request createFile, see here. For other requests sure. And even then, the server doesn't consider the project param in Form-Data as valid param. Only query param lead to different outcome.

I think I already searched the logs but nothing relates to it. But I'll double check in a few days.

And yes I did call setProjectand I also can authenticate the user etc...

I don't see where in code the projectId is set for the request createFile, see here. For other requests sure. And even then, the server doesn't consider the project param in Form-Data as valid param. Only query param lead to different outcome.

I think I already searched the logs but nothing relates to it. But I'll double check in a few days.

And yes I did call setProjectand I also can authenticate the user etc...

We don't set project id per request, it's already set in the header and every request use that header. Project param is not form field. Server accepts projectid as both header as well as query param. So if the project is set via header you don't have to worry that your request doesn't have it in query param.

This is a problem due to a quirk with how FormData and is implemented in RN (a File on the web is different than a File in RN).

For a workaround, see #27 (comment).

Closing this as a duplicate.