GithubHelp home page GithubHelp logo

Put file upload support about node-libcurl HOT 6 CLOSED

jcmais avatar jcmais commented on July 3, 2024
Put file upload support

from node-libcurl.

Comments (6)

JCMais avatar JCMais commented on July 3, 2024

CURLOPT_HTTPPOST.
All curl options

Some options are not implemented, like socket callbacks.

And your code is incorrect. Correct:

var Curl = require('node-libcurl').Curl;
console.log( Curl.option );

from node-libcurl.

Jakobud avatar Jakobud commented on July 3, 2024

Okay thanks! That is making more sense now. Do you know how to use the UPLOAD option? I'm trying to do something like this:

curl.setOpt('URL', url);
curl.setOpt('UPLOAD', true);
curl.setOpt('READDATA', fh);
curl.setOpt('INFILESIZE', fileSize);

But I get:

return this._curl.setOpt( optionIdOrName, optionValue );
                      ^
TypeError: Option value should be a string.

Specifically its referring to this line:

curl.setOpt('READDATA', fh);

I'm not sure why it's giving that error...

I'm following this type of example:

http://curl.haxx.se/libcurl/c/CURLOPT_UPLOAD.html

Any clues on this?

from node-libcurl.

Jakobud avatar Jakobud commented on July 3, 2024

Trying to look a little deeper into this, documentation says that the READDATA option should be a file pointer, which is what I'm doing:

var fh = fs.openSync('myfile.txt');

but your library is saying it should be a string. Any thoughts on that? Am I misunderstanding?

http://curl.haxx.se/libcurl/c/CURLOPT_READDATA.html

from node-libcurl.

JCMais avatar JCMais commented on July 3, 2024

As this is a binding for libcurl, in javascript, it has some limitations. Not all options are going to have the same type requirement, and not all options are going to be available.

READDATA is one of those. READDATA needs a file pointer that can be read by the libcurl READFUNCTION, it's not possible to pass a javascript file descriptor because of that. And it's not possible to change the READFUNCTION from javascript, since it requires a c function callback.

I will take a look into that to see what I can do. But I'm probably going to find some problems, like this one nodejs/node-gyp#416

So, unfortunately, PUT file uploads are currently not supported.

Edit:

If you can read the file content beforehand, you can use the following workaround and see if it works:

curl.setOpt( "CUSTOMREQUEST", "PUT"); 
curl.setOpt( "POSTFIELDS", fileContent );

from node-libcurl.

JCMais avatar JCMais commented on July 3, 2024

Ok, got it working without major issues, gonna test some more, and will push it to the dev branch this week.

from node-libcurl.

JCMais avatar JCMais commented on July 3, 2024

Done in v0.6.0.

Example:

var Curl = require( '../lib/Curl' ),
    path = require( 'path' ),
    fs   = require( 'fs' ),
    crypto = require( 'crypto' );

var curl = new Curl(),
    url = 'httpbin.org/put',
    fileSize = 10 * 1024, //1KB
    fileName = path.resolve( __dirname, 'upload.test' ),
    fd = -1;

//write random bytes to a file, this will be our upload file.
fs.writeFileSync( fileName, crypto.randomBytes( fileSize ) );

console.log( 'File: ', fs.readFileSync( fileName, 'base64' ) );

fs.open( fileName, 'r+', function( err, fd ) {

    //enabling VERBOSE mode so we can get more details on what is going on.
    curl.setOpt( Curl.option.VERBOSE, 1 );
    //set UPLOAD to a truthy value to enable PUT upload.
    curl.setOpt( Curl.option.UPLOAD, 1 );
    //pass the file descriptor to the READDATA option
    // passing one invalid value here will cause an aborted by callback error.
    curl.setOpt( Curl.option.READDATA, fd );

    curl.setOpt( Curl.option.URL, url );

    curl.on( 'end', function( statusCode, body ) {

        console.log( body );

        //remember to always close the file descriptor!
        fs.closeSync( fd );

        fs.unlinkSync( fileName );

        //the same for the curl instance, always close it when you don't need it anymore.
        this.close();
    });

    curl.on( 'error', function( err ) {

        console.log( err );

        fs.closeSync( fd );
        fs.unlinkSync( fileName );
        this.close();
    });

    curl.perform();
});

from node-libcurl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.