Comments (6)
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.
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.
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.
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.
Ok, got it working without major issues, gonna test some more, and will push it to the dev branch this week.
from node-libcurl.
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)
- How can I write a custom request asynchronously? HOT 1
- [feat] rewrite `node-libcurl` with, the **ONLY** isomorphic way, napi HOT 1
- Curl Easy segmentation fault with requests to external site HOT 3
- Expected npx node-libcurl $URL to work as curl $URL HOT 1
- TypeError: Class extends value #<Object> is not a constructor or null HOT 1
- HTTP_CONTENT_DECODING = false but still decodes the server's responses HOT 1
- How to make multiple curly instances maintain separate connections to same host? HOT 2
- unable to npm install HOT 3
- npm install issues. HOT 5
- libcurl vuln HOT 2
- Add prebuilt binaries for nodejs 20 LTS HOT 13
- read raw msg HOT 3
- Does node-libcurl support res_init? HOT 1
- Error after upgrade to Node 20.10 ...: ERR_DLOPEN_FAILED HOT 2
- SFTP / SSH stuck on login when using Curl/Multi and password auth HOT 2
- Curly - Close connection - Timeout Exception HOT 3
- Cannot install HOT 3
- Send binary data with HTTPPOST option HOT 1
- Hanging on PROTOCONNECTING
- Add prebuilt binaries for node 21(macOS) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-libcurl.