gotev / android-upload-service

Easily upload files (Multipart/Binary/FTP out of the box) in the background with progress notification. Support for persistent upload requests, customizations and custom plugins.
Apache License 2.0
2.83k stars 694 forks source link

How to add params to MultipartUploadRequest body? #483

Closed muhammadelkady25 closed 4 years ago

muhammadelkady25 commented 4 years ago

I have a server with an endpoint that takes a file and two parameters. It is working on postman as expected

Screen Shot 2019-12-28 at 5 18 47 PM

But it is not working with the library.

suspend fun upload(parentId: String, uris: List<Uri>) =
        withContext(this) {
            uris.forEach { uri ->
                MultipartUploadRequest(app, API.uploadUrl())
                    .addFileToUpload(filePath = uri.path!!, parameterName = "media")
                    .addParameter("parent_id", parentId)
                    .addParameter("title", "2KrYrNix2YrYqNmKINmm")
                    .startUpload()
            }
        }

The server returns Incomplete Post Data. which means one or more param is missing And here is the logs

2019-12-28 17:27:52.347 16679-16679/com.qadi.quran I/UploadService: NotificationActionsObserver - (uploadId: N/A) - registered
2019-12-28 17:27:52.366 16679-16679/com.qadi.quran I/UploadService: UploadService - (uploadId: N/A) - Starting UploadService. Debug info: {
        "uploadServiceVersion": "4.0.0-rc2",
        "androidApiVesion": 28,
        "namespace": "com.qadi.quran",
        "deviceProcessors": 8,
        "idleTimeoutSeconds": 10,
        "bufferSizeBytes": 4096,
        "httpStack": "net.gotev.uploadservice.network.hurl.HurlStack",
        "uploadProgressNotificationIntervalMillis": 333,
        "retryPolicy": {"initialWaitTimeSeconds": 1, "maxWaitTimeSeconds": 100, "multiplier": 2, "defaultMaxRetries": 3},
        "isForegroundService": true,
        "schemeHandlers": ["/": "class net.gotev.uploadservice.schemehandlers.FileSchemeHandler", "content://": "class net.gotev.uploadservice.schemehandlers.ContentResolverSchemeHandler"]
    }
2019-12-28 17:27:52.370 16679-16679/com.qadi.quran I/UploadService: UploadService - (uploadId: N/A) - Successfully created new task with class: net.gotev.uploadservice.protocols.multipart.MultipartUploadTask
2019-12-28 17:27:52.380 16679-16752/com.qadi.quran I/UploadService: UploadService - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - now holds foreground notification
2019-12-28 17:27:52.383 16679-16752/com.qadi.quran I/UploadService: MultipartUploadTask - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - Starting upload task
2019-12-28 17:27:52.384 16679-16752/com.qadi.quran I/UploadService: HurlStackRequest - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - creating new HttpURLConnection (uuid: 6ee1f3a5-e0c6-4dc2-a416-81287ff663df)
2019-12-28 17:27:52.389 16679-16752/com.qadi.quran I/UploadService: UploadTask - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - uploaded 0%, 138 of 630907 bytes
2019-12-28 17:27:53.213 16679-16752/com.qadi.quran I/UploadService: UploadTask - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - uploaded 77%, 487806 of 630907 bytes
2019-12-28 17:27:53.232 16679-16752/com.qadi.quran I/UploadService: UploadTask - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - uploaded 100%, 630907 of 630907 bytes
2019-12-28 17:27:55.959 16679-16752/com.qadi.quran I/UploadService: HurlStackRequest - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - closing HttpURLConnection (uuid: 6ee1f3a5-e0c6-4dc2-a416-81287ff663df)
2019-12-28 17:27:55.961 16679-16752/com.qadi.quran I/UploadService: MultipartUploadTask - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - Server response: code 200, body {"msg":"Incomplete Post Data."}
2019-12-28 17:27:55.961 16679-16752/com.qadi.quran I/UploadService: UploadTask - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - upload completed
2019-12-28 17:27:55.974 16679-16752/com.qadi.quran I/UploadService: UploadService - (uploadId: 3fb8ef09-b0a5-4885-baba-686d95386ba4) - now un-holded foreground notification
2019-12-28 17:27:55.974 16679-16752/com.qadi.quran I/UploadService: UploadService - (uploadId: N/A) - All tasks completed, stopping foreground execution
2019-12-28 17:27:55.978 16679-16752/com.qadi.quran I/UploadService: UploadService - (uploadId: N/A) - Service will be shut down in 10s if no new tasks are received
2019-12-28 17:28:05.981 16679-16763/com.qadi.quran I/UploadService: UploadService - (uploadId: N/A) - Service is about to be stopped because idle timeout of 10s has been reached
2019-12-28 17:28:05.996 16679-16679/com.qadi.quran I/UploadService: NotificationActionsObserver - (uploadId: N/A) - unregistered
2019-12-28 17:28:05.997 16679-16679/com.qadi.quran I/UploadService: UploadService - (uploadId: N/A) - Stopping foreground execution
2019-12-28 17:28:06.001 16679-16679/com.qadi.quran I/UploadService: UploadService - (uploadId: N/A) - UploadService destroyed
gotev commented 4 years ago

Hi @muhammadelkady25 I suspect the error is not in the library. I see 10 headers from the Postman screenshot which are not provided in the upload code. To debug the payloads which are sent, setup OkHttp Stack together with Stetho or Gander inspectors.

Why using a suspend fun? MultipartUploadRequest is not blocking in any way. It's simply a safe builder for upload service intents.

muhammadelkady25 commented 4 years ago

Thank you for your help @gotev I used Logging Interceptor. An OkHttp interceptor which logs HTTP request and response data. and here are my logs

D/OkHttp: --> POST https://qadi-quran.herokuapp.com/upload/media
D/OkHttp: Content-Length: 630897
D/OkHttp: Content-Type: multipart/form-data; boundary=-------UploadService4.0.0-rc2-21361348441798
D/OkHttp: Connection: close
D/OkHttp: ---------UploadService4.0.0-rc2-21361348441798
D/OkHttp: Content-Disposition: form-data; name="parent_id"
D/OkHttp: main-media
D/OkHttp: ---------UploadService4.0.0-rc2-21361348441798
D/OkHttp: Content-Disposition: form-data; name="title"
D/OkHttp: 2KrYrNix2YrYqNmKINmm
D/OkHttp: ---------UploadService4.0.0-rc2-21361348441798
D/OkHttp: Content-Disposition: form-data; name="media"; filename="-17.mp3"
D/OkHttp: Content-Type: audio/mpeg
D/OkHttp: ... long content ...
D/OkHttp: ---------UploadService4.0.0-rc2-21361348441798--
D/OkHttp: --> END POST (630897-byte body)
gotev commented 4 years ago

You're welcome 😃

Parameters set are correctly sent. Can't see evidence of Android Upload Service malfunction here.

Also, avoid uri.path!! in favor of uri.toString() 😃 !! is an unrecoverable crash point in Kotlin 💥

muhammadelkady25 commented 4 years ago

Thank you very much for your help. As it seems not an issue with Android Upload Service, I will close this issue.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related issues, referencing this one.