tus-android-client icon indicating copy to clipboard operation
tus-android-client copied to clipboard

Unexpected status code (400) while uploading chunk

Open krupasanghani023 opened this issue 8 months ago • 9 comments

When i try to upload video on cloudflare unable to upload it and get io.tus.java.client.ProtocolException: unexpected status code (400) while uploading chunk error.

To Reproduce Steps to reproduce the behavior:

  1. Select video and upload it
  2. If uploaded then please upload same video and check

Expected behavior We can upload duplicate video all time no restriction is required

Setup details

  • Used tus-android-client version: [e.g. :0.1.11]
  • Used tus-java-client version: [e.g. :0.5.0]

krupasanghani023 avatar Oct 19 '23 07:10 krupasanghani023

You are likely missing a CloudFlare-specific settings, such as a header or URL parameter. I have no experience with their services, so I cannot help you much here. Maybe this discussion can lead you to a solution: https://github.com/tus/tus-java-client/issues/66

Acconut avatar Oct 19 '23 07:10 Acconut

Have added header something like below one when i try to init tusclient. Is there any problem in it @Acconut?

try { SharedPreferences pref = getSharedPreferences("tus", 0); HashMap<String, String> hashMap = new HashMap<String, String>();

    hashMap.put("Content-Type", "application/json");
    hashMap.put("Authorization", "Bearer <token>");

    client = new TusClient();
    client.setHeaders(hashMap);
    client.setUploadCreationURL(new URL("https://api.cloudflare.com/client/v4/accounts/<account-id>/stream"));
    client.enableResuming(new TusPreferencesURLStore(pref));
} catch (Exception e) {
    showError(e);
}

krupasanghani023 avatar Oct 19 '23 08:10 krupasanghani023

Don't set Content-Type because tus-java-client has to change it. Also, I don't know if CloudFlare requires any other headers. Please look that up in their documentation.

Acconut avatar Oct 19 '23 08:10 Acconut

If we won't set Content-Type then also same error and Authentication header is require to cloudflare for authenticate it.

According to cloudflare doc it require below things:

curl -X POST \n -d '{"url":"<video_url>","meta":{"name":"<video_name>"}}' \n -H "Authorization: Bearer <api_token>" \n https://api.cloudflare.com/client/v4/accounts/<account_id>/stream

This attached ss is of error which we get with tusclient:

Screenshot 2023-10-19 at 2 27 55 PM

krupasanghani023 avatar Oct 19 '23 08:10 krupasanghani023

I don't know CloudFlare's API but your request looks different than from what is described in their docs: https://developers.cloudflare.com/api/operations/stream-videos-initiate-video-uploads-using-tus

Acconut avatar Oct 19 '23 09:10 Acconut

Have updated same headers and check still same error is there:

io.tus.java.client.ProtocolException: unexpected status code (400) while creating upload W/System.err: at io.tus.java.client.TusClient.createUpload(TusClient.java:211) W/System.err: at io.tus.java.client.TusClient.resumeOrCreateUpload(TusClient.java:334) W/System.err: at io.tus.android.example.MainActivity$UploadTask.doInBackground(MainActivity.java:177) W/System.err: at io.tus.android.example.MainActivity$UploadTask.doInBackground(MainActivity.java:132) W/System.err: at android.os.AsyncTask$3.call(AsyncTask.java:394) W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:264) W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)

krupasanghani023 avatar Oct 19 '23 09:10 krupasanghani023

The actual error message is probably available in the response body, but there is currently no easy way in tus-java-client to receive this body. That is something we have to improve in our API.

You could try the approach provided in https://github.com/tus/tus-java-client/issues/89 to try and receive the response body from CloudFlare.

Acconut avatar Oct 19 '23 11:10 Acconut

Screenshot 2023-10-20 at 10 01 53 AM

Have added this override method and get error as Bad request

krupasanghani023 avatar Oct 20 '23 04:10 krupasanghani023

I had hoped for a more descriptive error message. My knowledge ends here and the reason for the error is probably something Cloudflare-specific. Maybe your environment doesn't support PATCH, which requires tus-java-client to use X-HTTP-Method-Override, which may not be supported by Cloudflare: https://github.com/tus/tus-java-client/issues/66#issuecomment-1280052624

Acconut avatar Oct 20 '23 05:10 Acconut