apache / cordova-plugin-file-transfer

Apache Cordova File Transfer Plugin
https://cordova.apache.org/
Apache License 2.0
595 stars 888 forks source link

File Transfer HTTP Method #251

Open allanevargas opened 4 years ago

allanevargas commented 4 years ago

Is there any way to specify the HTTP method of the file transfer plugin?

I have this for my file upload:

        const options: FileUploadOptions = {
            fileKey: 'file',
            fileName: imageData.substr(imageData.lastIndexOf('/') + 1),
            headers: {},
            httpMethod: 'POST',
            chunkedMode: false,
            params: { action : 'INSERT', lineid : this.lineId }
        };

But it is being received as a GET request on my server side (PHP).

breautek commented 4 years ago

Are you sure you're using the upload api? For upload, the http method is forced to either PUT or POST.

See:

https://github.com/apache/cordova-plugin-file-transfer/blob/dc8fbd78a234bd88450ebe01835c60371bc2cf8e/www/FileTransfer.js#L127-L131

allanevargas commented 4 years ago

@breautek Certain. This is my code:

this.fileTransfer.upload(imageData, encodeURI(url.replace('https', 'http') + 'pages/attachments.php'), options).then(data => {
    console.log(data);
}, error => {
    console.log(error);
});
breautek commented 4 years ago

On my app, I'm using version 1.7.2-dev, and it appears to work for me, at least for iOS and android. I have my own fork, and I am 4 commits behind Apache's master.

Note that this plugin is deprecated so there won't be any new releases or PRs merged in.

If you open up your project in Xcode (platforms/ios/) or Android Studio (platforms/android/), you should be able to set breakpoints to ensure the requests are being sent as a POST request from the native code.

phoenix503 commented 3 years ago

Hi @breautek

Can you please suggest me the way how to add requestBody in fileTransfer plugin. I am adding it in params but it is not working giving error as "Missing fields are required"

FileTransferError {code: 1, source: "content://com.android.providers.media.documents/document/document%3A33", target: "http://192.168.1.6:9001/protected/", http_status: 400, body: "<?xml version="1.0" encoding="UTF-8"?>↵<Error><Cod…34ec-b728-4fb5-a2fb-1beb859876db</HostId></Error>", …}body: "<?xml version="1.0" encoding="UTF-8"?>↵<Error><Code>MissingFields</Code><Message>Missing fields in request.</Message><BucketName>protected</BucketName><Resource>/protected/</Resource><RequestId>168FD184D26AB3F2</RequestId><HostId>f83934ec-b728-4fb5-a2fb-1beb859876db</HostId></Error>"code: 1exception: "http://192.168.1.6:9001/protected/"http_status: 400source: "content://com.android.providers.media.documents/document/document%3A33"target: "http://192.168.1.6:9001/protected/"__proto__: Object


    let options: FileUploadOptions = {
            fileKey: 'file',
            fileName: res[0].filename,
            httpMethod: res[0].http_method,
            params: requestBodyObj,
            chunkedMode: false
        };
        this.fileTransfer
            .upload(filePath, encodeURI(res[0].upload_url), options)
            .then(
                data => {
                    console.log(data);
                },
                err => {
                    console.log(err);
                }
            );