cla/lwp - LWP User Agent
The LWP user agent is a powerful and resourceful alternative to the cla/web
module, which is lightweight and simpler.
lwp.agent(options)¶
Returns a new web agent object. The web agent object is analogous to a browser
or a curl
command, in a way that it can be used to craft elaborate web
requests.
In this example, we invoke a REST GET
webservice that returns JSON content.
var lwp = require('cla/lwp'); var ua = lwp.agent(); var res = ua.request('GET', 'http://example.com'); print(res.content());
Posting JSON content to an endpoint:
var lwp = require('cla/lwp'); var ua = lwp.agent(); var json = JSON.stringify({ foo: 1234, bar: 'hello' }); var res = ua.request('GET', 'https://jsonplaceholder.typicode.com/todos/1', { content_type: 'application/json', content: json }); print(res.content(), res.code()); var obj = JSON.parse(res.content()); print('UserID' + obj.userId);
Using digest authentication:
var lwp = require('cla/lwp'); var ua = lwp.agent(); ua.credentials('server:8080', 'MyRealm', 'myusername', 'mypassword'); var res = ua.request('GET', 'http://some.digest.example.com');
Attaching a file as POST in a form:
var lwp = require("cla/lwp"); var ua = lwp.agent(); var localFile = '/tmp/myfile.tar'; const res = ua.post( 'http://example.com/postfile', 'Content-Type', 'multipart/form-data', 'Content', { file: [localFile] } ); if( res.isSuccess() ) { print("Done!"); }
Downloading a file is now also possible, and is done automatically by the
cla/lwp
module by sending the full path to the file using the option toFile
in the request:
var lwp = require("cla/lwp"); var ua = lwp.agent(); const res = ua.request('GET', 'https://file-examples-com.github.io/uploads/2017/02/zip_10MB.zip', { toFile: '/tmp/localzip.zip' } ); if( res.isSuccess() ) { print("Done!"); }
You can also set toPath
to downloading files. In this case, the file name
will be extracted from the last part of the URL and appended to toPath
:
var lwp = require("cla/lwp"); var ua = lwp.agent(); // writes the file /tmp/zip_10MB.zip const res = ua.request('GET', 'https://file-examples-com.github.io/uploads/2017/02/zip_10MB.zip', { toPath: '/tmp' } ); if( res.isSuccess() ) { print("Done!"); }
More info¶
For a complete description of methods available, this library mimicks Perl's LWP::UserAgent.