Code Monkey home page Code Monkey logo

unirest-nodejs's Introduction

Unirest for Node.js Build Status

License Downloads Gitter

Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by Kong, who also maintain the open-source API Gateway Kong.

Installing

To utilize unirest for node.js install the the npm module:

$ npm install unirest

After installing the npm package you can now start simplifying requests like so:

var unirest = require('unirest');

Creating Requests

You're probably wondering how by using Unirest makes creating requests easier. Besides automatically supporting gzip, and parsing responses, lets start with a basic working example:

unirest
  .post('http://mockbin.com/request')
  .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
  .send({ "parameter": 23, "foo": "bar" })
  .then((response) => {
    console.log(response.body)
  })

Uploading Files

Transferring file data has been simplified:

unirest
  .post('http://mockbin.com/request')
  .headers({'Content-Type': 'multipart/form-data'})
  .field('parameter', 'value') // Form field
  .attach('file', '/tmp/file') // Attachment
  .then(function (response) {
    console.log(response.body)
  })

Custom Entity Body

unirest
  .post('http://mockbin.com/request')
  .headers({'Accept': 'application/json'})
  .send(Buffer.from([1,2,3]))
  .then(function (response) {
    console.log(response.body)
  })

Unirest

A request can be initiated by invoking the appropriate method on the unirest object, then calling .end() to send the request. Alternatively you can send the request directly by providing a callback along with the url.

unirest(method [, uri, headers, body, callback])

  • method - Request type (GET, PUT, POST, etc...)
  • uri - Optional; When passed will return a Request object. Otherwise returns generated function with method pre-defined (e.g. unirest.get)
  • headers (Object) - Optional; HTTP Request headers
  • body (Mixed) - Optional; HTTP Request body
  • callback (Function) - Optional; Invoked when Request has finalized with the argument Response

unirest[method](url [, headers, body, callback])

  • method - Request type, pre-defined methods, see below.
  • url - Request location.
  • headers (Object | Function) - Optional; When Object headers are passed along to the Request.header method, when Function this argument is used as the callback.
  • body (Mixed | Function) - Optional; When body is not a Function it will be passed along to Request.send() method, otherwise when a Function it will be used as the callback.
  • callback (Function) - Optional; Calls end with given argument, otherwise Request is returned.

All arguments above, with the exclusion of url, will accept a Function as the callback. When no callback is present, the Request object will be returned.

get

Returns a Request object with the method option set to GET

var Request = unirest.get('http://mockbin.com/request')

head

Returns a Request object with the method option set to HEAD

let Request = unirest.head('http://mockbin.com/request')

put

Returns a Request object with the method option set to PUT

let Request = unirest.put('http://mockbin.com/request')

post

Returns a Request object with the method option set to POST

let Request = unirest.post('http://mockbin.com/request')

patch

Returns a Request object with the method option set to PATCH

let Request = unirest.patch('http://mockbin.com/request')

delete

Returns a Request object with the method option set to DELETE

let Request = unirest.delete('http://mockbin.com/request')

unirest.jar()

Creates a container to store multiple cookies, i.e. a cookie jar.

let CookieJar = unirest.jar()
CookieJar.add('key=value', '/')

unirest
  .get('http://mockbin.com/request')
  .jar(CookieJar)

unirest.cookie(String)

Creates a cookie, see above for example.

unirest.request

mikeal/request library (the underlying layer of unirest) for direct use.

Request

Provides simple and easy to use methods for manipulating the request prior to being sent. This object is created when a Unirest Method is invoked. This object contains methods that are chainable like other libraries such as jQuery and popular request module Superagent (which this library is modeled after slightly).

Example

var Request = unirest.post('http://mockbin.com/request');

Request
  .header('Accept', 'application/json')
  .end(function (response) {
    ...
  })

Request Methods

Request Methods differ from Option Methods (See Below) in that these methods transform, or handle the data in a sugared way, where as Option Methods require a more hands on approach.

Request.auth(Object) or (user, pass, sendImmediately)

Accepts either an Object containing user, pass, and optionally sendImmediately.

  • user (String) - Authentication Username
  • pass (String) - Authentication Password
  • sendImmediately (String) - Optional; Defaults to true; Flag to determine whether Request should send the basic authentication header along with the request. Upon being false, Request will retry with a proper authentication header after receiving a 401 response from the server (which must contain a WWW-Authenticate header indicating the required authentication method)

Object

Request.auth({
  user: 'Nijiko',
  pass: 'insecure',
  sendImmediately: true
})

Arguments

Request.auth('Nijiko', 'insecure', true)

Request.header(header[, value])

Suggested Method for setting Headers

Accepts either an Object containing header-name: value entries, or field and value arguments. Each entry is then stored in a two locations, one in the case-sensitive Request.options.headers and the other on a private _headers object that is case-insensitive for internal header lookup.

  • field (String) - Header name, such as Accepts
  • value (String) - Header value, such as application/json

Object

Request.headers({
  'Accept': 'application/json',
  'User-Agent': 'Unirest Node.js'
})

Note the usage of Request.headers which is simply an alias to the Request.header method, you can also use Request.set to set headers.

Arguments

Request.header('Accept', 'application/json');

Request.part(Object)

Experimental

Similiar to Request.multipart() except it only allows one object to be passed at a time and does the pre-processing on necessary body values for you.

Each object is then appended to the Request.options.multipart array.

Request
  .part({
    'content-type': 'application/json',
    body: { foo: 'bar' }
  })
  .part({
    'content-type': 'text/html',
    body: '<strong>Hello World!</strong>'
  })

Request.query(Object) or (String)

Serializes argument passed to a querystring representation.

Should url already contain a querystring, the representation will be appended to the url.

unirest
  .post('http://mockbin.com/request')
  .query('name=nijiko')
  .query({
    pet: 'spot'
  })
  .then((response) => {
    console.log(response.body)
  })

Request.send(Object | String)

Data marshalling for HTTP request body data

Determines whether data mime-type is form or json. For irregular mime-types the .type() method is used to infer the content-type header.

When mime-type is application/x-www-form-urlencoded data is appended rather than overwritten.

JSON

unirest
  .post('http://mockbin.com/request')
  .type('json')
  .send({
    foo: 'bar',
    hello: 3
  })
  .then((response) => {
    console.log(response.body)
  })

FORM Encoded

// Body would be:
// name=nijiko&pet=turtle
unirest
  .post('http://mockbin.com/request')
  .send('name=nijiko')
  .send('pet=spot')
  .then((response) => {
    console.log(response.body)
  })

HTML / Other

unirest
  .post('http://mockbin.com/request')
  .set('Content-Type', 'text/html')
  .send('<strong>Hello World!</strong>')
  .then((response) => {
    console.log(response.body)
  })

Request.type(String)

Sets the header Content-Type through either lookup for extensions (xml, png, json, etc...) using mime or using the full value such as application/json.

Uses Request.header to set header value.

Request.type('application/json') // Content-Type: application/json
Request.type('json') // Content-Type: application/json
Request.type('html') // Content-Type: text/html

Request Form Methods

The following methods are sugar methods for attaching files, and form fields. Instead of handling files and processing them yourself Unirest can do that for you.

Request.attach(Object) or (name, path)

Object should consist of name: 'path' otherwise use name and path.

  • name (String) - File field name
  • path (String | Object) - File value, A String will be parsed based on its value. If path contains http or https Request will handle it as a remote file. If path does not contain http or https then unirest will assume that it is the path to a local file and attempt to find it using path.resolve. An Object is directly set, so you can do pre-processing if you want without worrying about the string value.

Object

unirest
  .post('http://mockbin.com/request')
  .header('Accept', 'application/json')
  .field({
    'parameter': 'value'
  })
  .attach({
    'file': 'dog.png',
    'relative file': fs.createReadStream(path.join(__dirname, 'dog.png')),
    'remote file': unirest.request('http://google.com/doodle.png')
  })
  .then((response) => {
    console.log(response.body)
  })

Arguments

unirest
  .post('http://mockbin.com/request')
  .header('Accept', 'application/json')
  .field('parameter', 'value') // Form field
  .attach('file', 'dog.png') // Attachment
  .attach('remote file', fs.createReadStream(path.join(__dirname, 'dog.png')))  // Same as above.
  .attach('remote file', unirest.request('http://google.com/doodle.png'))
  .then((response) => {
    console.log(response.body)
  })

Request.field(Object) or (name, value)

Object should consist of name: 'value' otherwise use name and value

See Request.attach for usage.

Request.stream()

Sets _stream flag to use request streaming instead of direct form-data usage. This seemingly appears to only work for node servers, use streaming only if you are a hundred percent sure it will work. Tread carefully.

Request.options

The options object is where almost all of the request settings live. Each option method sugars to a field on this object to allow for chaining and ease of use. If you have trouble with an option method and wish to directly access the options object you are free to do so.

This object is modeled after the request libraries options that are passed along through its constructor.

  • url (String | Object) - Url, or object parsed from url.parse()
  • qs (Object) - Object consisting of querystring values to append to url upon request.
  • method (String) - Default GET; HTTP Method.
  • headers (Object) - Default {}; HTTP Headers.
  • body (String | Object) - Entity body for certain requests.
  • form (Object) - Form data.
  • auth (Object) - See Request.auth() below.
  • multipart (Object) - Experimental; See documentation below.
  • followRedirect (Boolean) - Default true; Follow HTTP 3xx responses as redirects.
  • followAllRedirects (Boolean) - Default false; Follow Non-GET HTTP 3xx responses as redirects.
  • maxRedirects (Number) - Default 10; Maximum number of redirects before aborting.
  • encoding (String) - Encoding to be used on setEncoding of response data.
  • timeout (Number) - Number of milliseconds to wait before aborting.
  • proxy (String) - See Request.proxy() below.
  • oauth (Object) - See Request.oauth() below.
  • hawk (Object) - See Request.hawk() below
  • strictSSL (Boolean) - Default true; See Request.strictSSL() below.
  • secureProtocol (String) - See Request.secureProtocol() below.
  • jar (Boolean | Jar) - See Request.jar() below.
  • aws (Object) - See Request.aws() below.
  • httpSignature (Object) - See Request.httpSignature() Below.
  • localAddress (String) - See Request.localAddress() Below.
  • pool (Object) - See Request.pool() Below.
  • forever (Boolean) - Default undefined; See Request.forever() Below

Request Option Methods

Request.url(String)

Sets url location of the current request on Request.options to the given String

Request.url('http://mockbin.com/request');

Request.method(String)

Sets method value on Request.options to the given value.

Request.method('HEAD');

Request.form(Object)

Sets form object on Request.options to the given object.

When used body is set to the object passed as a querystring representation and the Content-Type header to application/x-www-form-urlencoded; charset=utf-8

Request.form({
  key: 'value'
})

Request.multipart(Array)

Experimental

Sets multipart array containing multipart-form objects on Request.options to be sent along with the Request.

Each objects property with the exclusion of body is treated as a header value. Each body value must be pre-processed if necessary when using this method.

Request.multipart([{
  'content-type': 'application/json',
  body: JSON.stringify({
    foo: 'bar'
  })
}, {
  'content-type': 'text/html',
  body: '<strong>Hello World!</strong>'
}])

Request.maxRedirects(Number)

Sets maxRedirects, the number of redirects the current Request will follow, on Request.options based on the given value.

Request.maxRedirects(6)

Request.followRedirect(Boolean)

Sets followRedirect flag on Request.options for whether the current Request should follow HTTP redirects based on the given value.

Request.followRedirect(true);

Request.timeout(Number)

Sets timeout, number of milliseconds Request should wait for a response before aborting, on Request.options based on the given value.

Request.timeout(2000)

Request.encoding(String)

Sets encoding, encoding to be used on setEncoding of response data if set to null, the body is returned as a Buffer, on Request.options based on given value.

Request.encoding('utf-8')

Request.strictSSL(Boolean)

Sets strictSSL flag to require that SSL certificates be valid on Request.options based on given value.

Request.strictSSL(true)

Request.httpSignature(Object)

Sets httpSignature

Request.proxy(String)

Sets proxy, HTTP Proxy to be set on Request.options based on value.

Request.proxy('http://localproxy.com')

Request.secureProtocol(String)

Sets the secure protocol to use:

Request.secureProtocol('SSLv2_method')
// or
Request.secureProtocol('SSLv3_client_method')

See openssl.org for all possible values.

Request.aws(Object)

Sets aws, AWS Signing Credentials, on Request.options

Request.aws({
  key: 'AWS_S3_KEY',
  secret: 'AWS_S3_SECRET',
  bucket: 'BUCKET NAME'
})

Request.oauth(Object)

Sets oauth, list of oauth credentials, on Request.options based on given object.

unirest
  .get('https://api.twitter.com/oauth/request_token')
  .oauth({
    callback: 'http://mysite.com/callback/',
    consumer_key: 'CONSUMER_KEY',
    consumer_secret: 'CONSUMER_SECRET'
  })
  .then(response => {
    let access_token = response.body

    return unirest
      .post('https://api.twitter.com/oauth/access_token')
      .oauth({
        consumer_key: 'CONSUMER_KEY',
        consumer_secret: 'CONSUMER_SECRET',
        token: access_token.oauth_token,
        verifier: token: access_token.oauth_verifier
      })
  })
  .then((response) => {
    var token = response.body

    return unirest
      .get('https://api.twitter.com/1/users/show.json')
      .oauth({
        consumer_key: 'CONSUMER_KEY',
        consumer_secret: 'CONSUMER_SECRET',
        token: token.oauth_token,
        token_secret: token.oauth_token_secret
      })
      .query({
        screen_name: token.screen_name,
        user_id: token.user_id
      })
  })
  .then((response) => {
    console.log(response.body)
  })

Request.hawk(Object)

Sets hawk object on Request.options to the given object.

Hawk requires a field credentials as seen in their documentation, and below.

Request.hawk({
  credentials: {
    key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
    algorithm: 'sha256',
    user: 'Steve'
  }
})

Request.localAddress(String)

Sets localAddress, local interface to bind for network connections, on Request.options

Request.localAddress('127.0.0.1')
Request.localAddress('1.2.3.4')

Request.jar(Boolean) or Request.jar(Jar)

Sets jar, cookie container, on Request.options. When set to true it stores cookies for future usage.

See unirest.jar for more information on how to use Jar argument.

Request.pool(Object)

Sets pool object on Request.options to the given object.

A maxSockets property can also be provided on the pool object to set the max number of sockets for all agents created.

Note that if you are sending multiple requests in a loop and creating multiple new pool objects, maxSockets will not work as intended. To work around this, create the pool object with the maxSockets property outside of the loop.

poolOption = { maxSockets: 100 }

Request.pool poolOption

Request.forever(Boolean)

Sets forever flag to use forever-agent module. When set to true, default http agent will be replaced by forever-agent, which keeps socket connections alive between keep-alive requests.

Request.forever(true);

Request.then(Function callback)

Promise polyfill method. Wraps Request.end in a Promise and will resolve or reject based on the result of the request.

unirest
  .get('http://mockbin.com/request')
  .then((response) => {
    console.log(response)
  })
  .catch(err => {
    console.log(err)
  })

Request.end(Function callback)

Sends HTTP Request and awaits Response finalization. Request compression and Response decompression occurs here. Upon HTTP Response post-processing occurs and invokes callback with a single argument, the [Response](#response) object.

unirest
  .get('http://mockbin.com/request')
  .end((response) => {
    console.log(response)
  })

Request Aliases

Request.set

Alias for Request.header()

Request.headers

Alias for Request.header()

Request.redirects

Alias for Request.maxRedirects()

Request.redirect

Alias for Request.followRedirect()

Request.ssl

Alias for Request.strictSSL()

Request.ip

Alias for Request.localAddress()

Request.complete

Alias for Request.end()

Request.as.json

Alias for Request.end()

Request.as.binary

Alias for Request.end()

Request.as.string

Alias for Request.end()

Response

Upon ending a request, and receiving a Response the object that is returned contains a number of helpful properties to ease coding pains.

General

  • body (Mixed) - Processed body data
  • raw_body (Mixed) - Unprocessed body data
  • headers (Object) - Header details
  • cookies (Object) - Cookies from set-cookies, and cookie headers.
  • httpVersion (String) - Server http version. (e.g. 1.1)
  • httpVersionMajor (Number) - Major number (e.g. 1)
  • httpVersionMinor (Number) - Minor number (e.g. 1)
  • url (String) - Dependant on input, can be empty.
  • domain (String | null) - Dependant on input, can be empty.
  • method (String | null) - Method used, dependant on input.
  • client (Object) - Client Object. Detailed information regarding the Connection and Byte throughput.
  • connection (Object) - Client Object. Specific connection object, useful for events such as errors. Advanced
  • socket (Object) Client Object. Socket specific object and information. Most throughput is same across all three client objects.
  • request (Object) - Initial request object.
  • setEncoding (Function) - Set encoding type.

Status Information

  • code (Number) - Status Code, i.e. 200
  • status (Number) - Status Code, same as above.
  • statusType (Number) - Status Code Range Type
    • 1 - Info
    • 2 - Ok
    • 3 - Miscellaneous
    • 4 - Client Error
    • 5 - Server Error
  • info (Boolean) - Status Range Info?
  • ok (Boolean) - Status Range Ok?
  • clientError (Boolean) - Status Range Client Error?
  • serverError (Boolean) - Status Range Server Error?
  • accepted (Boolean) - Status Code 202?
  • noContent (Boolean) - Status Code 204 or 1223?
  • badRequest (Boolean) - Status Code 400?
  • unauthorized (Boolean) - Status Code 401?
  • notAcceptable (Boolean) - Status Code 406?
  • notFound (Boolean) - Status Code 404?
  • forbidden (Boolean) - Status Code 403?
  • error (Boolean | Object) - Dependant on status code range.

response.cookie(name)

Sugar method for retrieving a cookie from the response.cookies object.

var CookieJar = unirest.jar();
CookieJar.add(unirest.cookie('another cookie=23'));

unirest.get('http://google.com').jar(CookieJar).end(function (response) {
  // Except google trims the value passed :/
  console.log(response.cookie('another cookie'));
});

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Made with ♥ from the Kong team

unirest-nodejs's People

Contributors

adamdunkley avatar anklos avatar bobjflong avatar cosmicflame avatar crtag avatar davidgatti avatar dpjanes avatar erbridge avatar harsimranmaan avatar hatemogi avatar icreatejb avatar jmaxxz avatar mjschranz avatar nijikokun avatar patrimart avatar pdehaan avatar rprieto avatar sedouard avatar sonicaghi avatar srushti avatar subnetmarco avatar thibaultcha avatar timothy-r avatar vot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unirest-nodejs's Issues

Comment fix

Line # 883 should be changed to CRLF
Current:
// Trailing CLRF
lines.pop();

Fix:
// Trailing CRLF
lines.pop();

Attaching relative or absolute files

Hiya,

I'm trying to attach a file to a request and running into some problems with building a file path that unirest will handle in a sensible manner.

On Linux I have either:

  • relative paths: data/file.txt
  • absolute paths: /opt/myapp/data/file.txt
    (where the cwd is /opt/myapp)

On Windows my paths look like:

  • relative paths: data\file.txt
  • absolute paths: C:\myapp\data\file.txt
    (where the cwd is C:\myapp)

Unirest expects file paths in one of three formats:

  • starting with http:// or https:// for remote files (which I'm not trying to do)
  • containing :// for absolute paths - which doesn't match up to what either Linux or Windows absolute paths would look like
  • neither of the above for relative paths - these come out as relative to __dirname, which is normally ./node_modules/unirest, a highly unlikely place to want to upload files from.

I've summed it up in the following Gist:
https://gist.github.com/cosmicflame/dd4bf009f6f7ababaa54
(Apologies if it's somewhat rambling... it's quite late where I am)

The two solutions I've found for forcing absolute paths are:

  1. C://myapp/data/file.txt - that is, forcing a :// in there, even though that's not platform-native. Yuck.
  2. path.relative('./node_modules/unirest', myAbsolutePath) - that is, turning my absolute path into a relative path that's relative to where unirest is installed. Double yuck.

The code that's doing the file loading is this:

if (does(item.value).contain('http://') || does(item.value).contain('https://')) {
  item.value = Unirest.require(item.value);
} else if (does(item.value).contain('://')) {
  item.value = fs.createReadStream(item.value);
} else {
  item.value = fs.createReadStream(path.join(__dirname, item.value));
}

I think we should ditch the :// check (unless I'm missing something here...) and instead use path.resolve() (http://nodejs.org/api/path.html) for both absolute and relative paths. Something like this:

if (does(item.value).contain('http://') || does(item.value).contain('https://')) {
  item.value = Unirest.require(item.value);
} else {
  item.value = fs.createReadStream(path.resolve(item.value));
}

This will turn both absolute and relative paths into absolute paths, which will then be read from disk correctly, on both Windows and Linux.

I'll happily make the change and send you a pull request if you'd be willing to accept it.

Thanks,

B~

Set or Get cookies

function cookieTeste(){
var CookieJar2 = unirest.jar();
CookieJar2.add(unirest.cookie('some value'));
unirest.get('http://httpbin.org/get')
.jar(CookieJar2)
.end(function(response){
         console.log('response = '+response.body)
        });
}

return

url.js:107
    throw new TypeError("Parameter 'url' must be a string, not " + typeof url)
          ^
TypeError: Parameter 'url' must be a string, not undefined

Have any mistake in usage sample ?

CookieJar3.add('PHPSESSID=fct9jmmlrputlmr1lb1hree3s3','http://httpbin.org/');
unirest.get('http://httpbin.org/cookies')
.jar(CookieJar3)
.end(function(response){
         console.log(CookieJar3)
         console.log('-----------------------------------')
         console.log(response.cookie('PHPSESSID'))
});

Work correctly to add cookie..
but i cant get cookie using usage sugestions.

How see cookies in unirest.jar() return a new cookie container ? ( so i can hold 2 3 or 4 set of cookies in my instance.. ? )

Expose request.defaults

Hiya,

Like many corporate drones, I'm stuck behind an HTTP proxy. This means that I need to code my apps to work in two different environments - on my laptop behind the proxy, and on the server without the proxy.

Because of this, it's very nice to be able to configure my proxy settings in one place instead of having to do it on every request. The request (https://github.com/mikeal/request) library offers request.defaults, which allows you to set the proxy as a default, e.g.

var r = request.defaults({'proxy':'http://localproxy.com'})

http.createServer(function (req, resp) {
  if (req.url === '/doodle.png') {
    r.get('http://google.com/doodle.png').pipe(resp)
  }
})

(Example from the request docs)

It would be really good to be able to set the proxy default for unirest in a similar manner.

What are your thoughts? I don't mind having a hack at implementing it if you'll accept a pull request.

Thanks,

B~

Correct use of Unirest.pipe

Hi, it's me again!

Quick question for you - I noticed that Unirest.pipe is defined as an alias to Unirest.request.pipe:
https://github.com/Mashape/unirest-nodejs/blob/master/index.js#L703
Unirest.pipe = Unirest.request.pipe;
This does not appear to documented in the readme.

I'm trying to download images using Unirest and have stumbled across a method that seems to work:

var unirest = require('unirest')
var fs = require('fs')

unirest.get("https://www.google.co.uk/logos/doodles/2014/dorothy-hodgkins-104th-birthday-born-1910-5134139112030208.3-hp.jpg")
  .end()
  .pipe(fs.createWriteStream(__dirname + '/myImg.jpg'))

Is this how Unirest.pipe is intended to be used, chained after .end(), or am I doing it wrong?

Thanks.

attach [http || https] error

.attach('u', 'http://google.com/blabla.png')

error: has no method 'require'
at Object.$this.end (...\node_modules\unirest\index.js:431:38)

 if (does(item.value).contain('http://') || does(item.value).contain('https://')) {
                item.value = Unirest.require(item.value);
              }

Unirest wont install

When i run npm install unirest

npm install unirest
npm http GET https://registry.npmjs.org/unirest
npm http 304 https://registry.npmjs.org/unirest
npm http GET https://registry.npmjs.org/form-data
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/request
npm http 304 https://registry.npmjs.org/form-data
npm http 304 https://registry.npmjs.org/request
npm ERR! Error: No compatible version found: form-data@'^0.2.0'
npm ERR! Valid install targets:
npm ERR! ["0.0.0","0.0.2","0.0.3","0.0.4","0.0.5","0.0.6","0.0.7","0.0.8","0.0.9","0.0.10","0.1.0","0.1.1","0.1.2","0.1.3","0.1.4","0.2.0"]
npm ERR! at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:685:10)
npm ERR! at /usr/local/lib/node_modules/npm/lib/cache.js:607:10
npm ERR! at saved (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/get.js:138:7)
npm ERR! at Object.oncomplete (fs.js:107:15)
npm ERR! If you need help, you may report this log at:
npm ERR! http://github.com/isaacs/npm/issues
npm ERR! or email it to:
npm ERR! [email protected]

npm ERR! System Linux 2.6.32-279.el6.x86_64
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "unirest"
npm ERR! cwd /var/www/html/smpp
npm ERR! node -v v0.10.4
npm ERR! npm -v 1.2.18
npm http 304 https://registry.npmjs.org/mime
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /var/www/html/smpp/npm-debug.log
npm ERR! not ok code 0

how to get body without callback?

var unirest = require('unirest');
function robotreply(FromUserName, content) {
    var msg;
    unirest.get('http://127.0.0.1/openapi/api?key=91e71ab13d4f1be71de4a7ef1c1ec2ef&info=' + content + 'userid=' + FromUserName).end(function (response) {
        msg = response.body;
    });
    return msg;
}
var msg = robotreply("YSTYLE", "你好!");
console.log(msg);

how to get body without callback?
I want to get the returned body, then for other code.
use callback it don't wait for returns

Multipart attachment issue

Hi there,
I was looking the documentation here:
http://unirest.io/nodejs.html

about the Request.attach(Object) or (name, path) section.
As I see there are three ways to attach a file (I tried the first two):
.attach({
(1) 'file': 'dog.png',
(2) 'relative file': fs.createReadStream(path.join(__dirname, 'dog.png'),
(3) 'remote file': unirest.request('http://google.com/doodle.png')
}).

I found a bug on the second way using the fs.createReadStream way. Basically I don't know if it is a bug or you would like to drop the functionality because as I see it is also not tested. Anyway I will try to explain. Using the second way I get back this error:
" Uncaught Error: ENAMETOOLONG, open '/home/svabael/projects/unirest-nodejs/tests/{"_readableState":{"highWaterMark":65536,"buffer":[],"length":0,"pipes":null,"pipesCount":0,"flowing":false,"ended":false,"endEmitted":false,"reading":false,"calledRead":false,"sync":true,"needReadable":false,"emittedReadable":false,"readableListening":false,"objectMode":false,"defaultEncoding":"utf8","ranOut":false,"awaitDrain":0,"readingMore":false,"decoder":null,"encoding":null},"readable":true,"domain":null,"_events":{},"_maxListeners":10,"path":"/home/cambridge/projects/unirest-nodejs/README.md","fd":null,"flags":"r","mode":438,"autoClose":true}' ".

As I see in the handleField function (line 625 - index.js) possibly we have to check if the value is an object because finally this is what the fs.createReadStream returns and send as parameter in the handleFieldValue the value.path. As a result we assign as the value of the field instead of a string, that is the path but the whole object.
And then in the handleFormData function (508 line - index.js) we are assigning the whole object as a parameter into the fs.createReadStream function that finally throws the error above. I don' t know if you need to drop this functionality or fix it. If you need to fix it and not to drop it just let me know and I can create a patch and test for this.

Thank you for your time!

multipart support doesnt seem to work

Say I have this code:

var request = unirest.post(endpoint);
request.part({
'Content-Disposition': 'file; filename="inflation.tiff"',
'content-type': 'image/tiff',
'Content-Transfer-Encoding' : 'base64',
body: base64Image
});

Couple of things:

  1. index.js looks for "content-type" which is case sensitive.
  2. This code errors out with:
    /home/jnankin/node_modules/unirest/index.js:321
    $this.options.multipart.push(options);
    ^
    TypeError: Cannot call method 'push' of undefined

Looking at index.js, I don't see where $this.options.multipart would ever have been set. It's only set if !$this._multipart, but _multipart is definied by default.

Cookie seems not working

Hi,
I chose Node.js over Java, trying to have some fun.
I found this awesome library on Internet, but with limited documentation, it's really difficult to explore (Sometimes I look into the source code and nail it, but I'm not familiar with the "http" package.).
Here's the issue:
When I copy and paste the very code from readme,

var CookieJar = unirest.jar();
CookieJar.add(unirest.cookie('some value'));
unirest.get('http://httpbin.org/get').jar(CookieJar);

something go wrong

CookieJar.add(unirest.cookie('some value'));
^
TypeError: Object # has no method 'add'

I wish to implement a login module and then keep the cookie so that I can do something with it .
How can I do that?

Best Regards,
Rowan

TypeError in response parsing.

Hi there,

I'm getting the following error on submitting a request:

TypeError: Cannot call method 'split' of undefined
at Function.Unirest.type (/Users/timb/Documents/node-v0.10.26-darwin-x86/SkyHTTPServices/node_modules/unirest/index.js:561:23)

Thanks in advance.

Tim

Cache support

Hi,

It would be great if unirest had support for a pluggable cache, just like the browser's local cache.

I see many packages on npm that support specifying a TTL for each request - but I'd much rather take advantage of HTTP and actually read the response headers, and cache the response in memory for the appropriate amount of time (either expire, or max-age - age).

In a sense, this is very similar to a reverse-proxy. Do you think this could be part of unirest? Could it be some sort of plugin, where every request/response traverses the proxy and it decides whether to call the backend or returned cached data?

If the interface is nice & clean, I could imagine several implementation of that proxy, e.g. in memory, backed by Redis... Ideally they would each have smart limits depending on their constrains (memory limit, number of responses, LRU....).

Happy to help implement this if you think it would be useful.

Type Error when using express

Hello,

I encountered a problem while using unirest with the node.js express framework.
As a standalone js file it works when I start it via the CLI.

index.js

exports.test = function(req, res){
test.getData();
};

test.js

var unirest = require('unirest');
var _ = require('underscore');

exports.getData = function(){

    var requestProject = JSON.stringify({
      "SERVICE": "",
      "LIMIT": "100",
      "OFFSET" : "0"
    });

    var headers = {
      'Authorization': 'Basic ' + new Buffer(username + ':' + api).toString('base64'),
      'Content-Type' : 'application/json;charset=utf-8',
      'Accept' : 'application/json'
    };
    test = function() {
        unirest.post(url)
        .type('json')
        .headers(headers)
        .send(requestProject)
        .end(function (response) {
            var jsonObj = JSON.parse(response.body); 
            result.push(jsonObj)
            finished();
      });

    }
    test();

}

Error:

500 TypeError: Object function ( needle ) {
for (i in this) {
if (this[i] == needle) return true;
}
return false;
} has no method 'indexOf'

cURL - data binary

Hi,

I was trying to simulate the curl below with unirest without success:

curl -u 4cb58534-0bf5-45a4-b009-92cc7b66b0a5:rfspLRFj1MnU -X POST --limit-rate 40000 --header "Content-Type: audio/wav" --header "Transfer-Encoding: chunked" --data-binary @01.wav "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true"

I can get everything done, except for the --data-binary @01.wav.

I've tried with the 'attach' method, but this service doesnt accept the file via multipart-form.

(the curl is working fine, I've executed a few times here and I'm getting the correct response)

Any thoughts? Current code below

            unirest.post('https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true&model=' + language)
              .headers({'content-type': 'audio/wav',
                        'transfer-encoding': 'chunked',
                        'content-length': fileSizeInBytes,
                        'authorization': 'Basic NGNiNTg1MzQtMGJmNS00NWE0LWIwMDktOTJjYzdiNjZiMGE1OnJmc3BMUkZqMU1uVQ=='})
              .attach('file', './uploads/' + randomName) // Attachment
              .as.binary(function (response) {
                console.log(response.body);
              });

Thanks in advance.

Mocking end call with with sinon throws "Cannot stub non-existent own property end"

var unirest     = require('unirest');

beforeEach(function() {
    // create a sandbox
    sandbox = sinon.sandbox.create();

    // stub some console methods
    sandbox.stub(unirest, "end", function (cb) {
        var res = {
            code: 200
        }
    }
}

throws:
TypeError: Cannot stub non-existent own property end
at Object.stub (/Users/nmelo/Desktop/node_modules/sinon/lib/sinon/collection.js:93:35)
at Context. (/Users/nmelo/Desktop/test/tests_test.js:25:13)
at Hook.Runnable.run (/Users/nmelo/Desktop/node_modules/mocha/lib/runnable.js:221:32)
at next (/Users/nmelo/Desktop/node_modules/mocha/lib/runner.js:259:10)
at Immediate._onImmediate (/Users/nmelo/Desktop/node_modules/mocha/lib/runner.js:276:5)
at processImmediate as _immediateCallback

Any idea how to stub the actual HTTP calls for testing?

Plugin Support / Hooks

Related to #47


Potential API:

  • .plugin(specification)

Other means of achieving this could be done through the stream api such as .on and .pipe methods

unirest doesn't appear to properly format a request message for uploading a file

I have a service running at http://webapi-14.apphb.com/api/values

You can post to it with a file attachment and it will echo the file back to you. But the request I create with unirest cannot be processed by the server (I just get back a 500). Here is my sample script.

var unirest = require('unirest');

unirest.post('http://webapi-14.apphb.com/api/values')
    .attach('file', 'square.txt')
    .end(function(response) {
        console.dir(response);
    });

I captured the request with wireshark. It doesn't have a content-length header and just has some other random "noise" like the numbers 96, 10, 38, and 0.

POST /api/values HTTP/1.1

host: webapi-14.apphb.com

content-type: multipart/form-data; boundary=--------------------------522656337014588850083523

Connection: keep-alive

Transfer-Encoding: chunked



96

----------------------------522656337014588850083523

Content-Disposition: form-data; name="file"; filename="square.txt"

Content-Type: text/plain






10

+--+

|  |

+--+

38



----------------------------522656337014588850083523--

0

The service does respond properly when submitted via curl or a C# client. To test, create a small text file. I created one called Square.txt

$ cat Square.txt
+--+
|  |
+--+

Then use curl to post this file

$ curl -F "file=@./Square.txt" http://webapi-14.apphb.com/api/values
+--+
|  |
+--+

Here is the good request created by curl:

POST http://webapi-14.apphb.com/api/values HTTP/1.1
Proxy-Authorization: Basic Y3dlbGNobWk6Um9ja3NvbGUwOTEy
User-Agent: curl/7.30.0
Host: webapi-14.apphb.com
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 204
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------01c08465f272

HTTP/1.1 100 Continue

------------------------------01c08465f272
Content-Disposition: form-data; name="file"; filename="Square.txt"
Content-Type: text/plain

+--+
|  |
+--+
------------------------------01c08465f272--

The server is defined by a mostly default C# WebApi project. The source is here: https://github.com/michaelgwelch/WebApi

The relevant code to process this request is in https://github.com/michaelgwelch/WebApi/blob/master/DefaultWebApiProject/Controllers/ValuesController.cs and looks like:

        // POST api/values
        public async Task<HttpResponseMessage> Post(HttpRequestMessage message)
        {
            var provider = new MultipartMemoryStreamProvider();
            await message.Content.ReadAsMultipartAsync(provider);

            var fileContent = provider.Contents.FirstOrDefault(content => content.Headers.ContentDisposition.Name.Trim('\"') == "file");
            if (fileContent == null) return new HttpResponseMessage(HttpStatusCode.BadRequest);

            var fileBytes = await fileContent.ReadAsByteArrayAsync();

            var response = new HttpResponseMessage(HttpStatusCode.OK) {Content = new ByteArrayContent(fileBytes)};
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
//            var fileName = Path.GetFileNameWithoutExtension(fileContent.Headers.ContentDisposition.FileName);
//            fileName = Path.ChangeExtension(fileName, ".png");

            response.Content.Headers.ContentDisposition.FileName = fileContent.Headers.ContentDisposition.FileName;

            response.Content.Headers.ContentType = fileContent.Headers.ContentType;

            return response;
        }

Root of the problem, I think, is the missing content-length header and other strange chars in the request.

Streaming support?

I'm in love with the library, but what about something important in node as streaming?

You can find a good case of use in request streaming

maybe you aren't interested in because is more difficult tu add support for this feature in all the platform compatibles with the library, but I have to ask! :-)

Request.as.json doesn't do a thing

Request.as.json is simply an alias for Request.end.

Facebook's Graph API, just to cite a popular instance, sends content-type text/javascript by default, which means unirest won't automatically parse the response body.

It'd be useful if instead of being an alias, Request.as.json(cb) was the equivalent of:

Request.header('Accept', 'application/json').end(cb)

Which fixes more than a few instances of non standard content-types being used.

Would you guys fancy a pull request?

PS: Don't know what should/could be done about Request.as.binary?

Exception when specifying Content-Type header

If one specifies a Content-Type header, an unhandled exception will be thrown from here:

https://github.com/Mashape/unirest-nodejs/blob/master/index.js#L527

Cannot call method 'split' of undefined

Basically, the local variable header is the value of the content-type header, so trying to use it as the index into the headers array is incorrect -- and returns an undefined object, which you can't call .split() on.

Seems like the correct fix is to just call header.split() directly?

HTTP digest wrong HA1

unirest gives request.js a lower case HTTP method name, i.e. get instead of GET, which causes a faulty HTTP digest auth.

Post request sends no data

var postdata = { 'lead[name]': 'Justin McNally',
  'lead[company_name]': 'Kohactive',
  'lead[email]': '[email protected]',
  'lead[phone]': '5742380887',
  'lead[responses_attributes][2][field_id]': '2',
  'lead[responses_attributes][2][data]': 'NeatProject',
  'lead[responses_attributes][3][field_id]': '3',
  'lead[responses_attributes][3][data]': '100',
  'lead[responses_attributes][4][field_id]': '4',
  'lead[responses_attributes][4][data]': '100000',
  'lead[responses_attributes][5][field_id]': '5',
  'lead[responses_attributes][5][data]': 'yolo',
  'lead[responses_attributes][6][field_id]': '6',
  'lead[responses_attributes][6][data]': 'lrdiv' }
 unirest.post('http://leadpophq.com/external/forms/asdasdas')
      .headers({"Referrer": "http://kohactive.com/", 'Accept': 'application/json' })
      .send(postdata)
      .end(function(response) {
        console.log(response)
      })

results in my server recieving no data.

node v4 support?

Seems it's not supported under node v4.

./src/util-inl.h:196: TypeName* node::Unwrap(v8::Local<v8::Object>) [with TypeName = node::TLSWrap]: Assertion `(object->InternalFieldCount()) > (0)' failed.
Aborted (core dumped)

Error :: has no method 'indexOf' using unirest.post() method

Hello @nijikokun ,

Am getting below error while using unirest.post() method:
TypeError: Object function (k) {
for (var i = 0; i < this.length; i++) {
if (this[i] === k) {
return true;
}
}
return false;
} has no method 'indexOf'
at Function.unirest [as post] at /node_modules/unirest/index.js:618:18

Here is how am using:

unirest.post(orderUrl)
.headers(headers)
.field('access_token', accessToken)
.attach('entry', fs.createReadStream(info.path))
.end(function (response) {
console.log(response.body);
});

Please advise on this issue. Thanks in advance.

Global proxy setting?

Hi! Love your library, using it all over the place. I had a quick Q though:

I use Charles as a local request inspector, which I end up routing in your library using .proxy('http://localhost:8888').strictSSL(false) , but I was wondering if there is a way to set this globally, and perhaps conditionally based off the environment variable? I'm finding I'm using it on a lot of my requests, and am needing to go and clean them all out when I push my code back up to production.

Lemme know :) Thank again for such a great library!

Custom JSON Content-Type on response avoids json parsing

Hello, I found this issue in which if you set a custom Content-Type header on the response like for example application/vnd.api+json then the response body is not parsed as JSON when checking the unirest's response, even if you use the provided type method to allow different content types.
I made a gist for this that reproduces the issue.

Instructions for gist:

 git clone https://gist.github.com/8bea1437f615c0c64163.git
 cd 8bea1437f615c0c64163
 npm install
 npm test

Gist source on https://gist.github.com/nicosommi/8bea1437f615c0c64163

query() incorrectly replacing all back slashes to forward slashes

It makes sense for query() to replace something like "http:\google.com" to "http://google.com", but it doesn't make sense for it to replace the querystring portion as well:

Input: "http:\google.com?q=hyphens-escaped-in-solr-like-this"
Desired Output: "http://google.com?q=hyphens%5C-escaped%5C-in%5C-solr%5C-like%5C-this"
Actual Output: "http://google.com?q=hyphens/-escaped/-in/-solr/-like/-this"

When making a request to a solr instance in this case, it malforms the request. I had attempted to use querystring parse and stringify to repack the request already url encoded with limited success - it worked for the one case but broke other cases. My workaround in the end was to simply replace the one character to %5C manually - ugly but works for now.

multipart/form-data doesn't send cookies

I use a session cookie to authenticate the API client, but if the client tries to upload a file to the server, the server receives no cookies (I've debugged server-side to be certain) and so it denies access.
Everyhting works fine if the request is not multipart/form-data.
The file does get sent successfully, though.

how to use the cookies?

var unirest = require('unirest'),
    baseURL = "http://www.gzviewit.com/pmp/";

unirest.post(baseURL + "index.php/Login/login.do")
    .field('username', 'fengwh') // Form field
    .field('password', '******') // Form field
    .field('remembername', '1') // Form field
    .jar(true)
    .end(function (response) {
        console.log(response.body);
        var cookies = response.cookies;
        console.log(cookies);
        unirest.get(baseURL + 'index.php/ProjectIndex/index.dot')
            .header("Cookie", cookies)
            .end(function (data) {
                console.log(data.body);
            });
    });

i use this, bug it don't work

cannot get 'http-only; secure" cookie from res.cookies

Hi there,

I am trying to read cookies from a get response using res.cookies. I can get most cookies as I get from browser, except the cookie marked as http-only and secure.

So my question is why the http-only; secure cookie can be seen in browser cookies but not in res.cookies?

Thanks.

Incorrect cookie parse

unirest/index.js 380:21

var crumbs = cookie.split('=');

I think it must be regexp here, because I can have cookies containing "=" symbol, so it will be incorrect value.

Or just

result.cookies[Unirest.trim(crumbs[0])] = Unirest.trim(crumbs.slice(1).join('=') || '');

Unirest stuck in a loop

I have two servers on NodeJS

  • one creates posts requests using Unrest (sending file)
  • second accepts them using Multer (receiving file).

On the receiving server after getting many requests the server will start to log POST / - - ms - -. When this happens Unirest will get a undefined status. My log looks like this Worker: 18208 - File didn't reach the server. Response: undefined.

Instead of dropping the connection, Unirest keeps sending the request even if my code finished working.

Unirest will stop this behavior if I kill the receiving server.

Any idea what is happening?

Rename enum mapping

  • Change enum to constants as it makes more sense when browsing / reading code.
  • Follow Java naming conventions for constants.

Header/Type not working correctly

Hello, following the README.md Create Request section I found that there is an issue setting the content-type for a post request. Tried both with type method and the header method. I made a gist for it.

Steps to reproduce it

  1. git clone https://gist.github.com/6917b308ecaf1a64fd5f.git
  2. npm install
  3. DEBUG=* npm test

You will see that the body parser says that the request has the content type set to "application/x-www-form-urlencoded"

Memroy leak

hi guys I'm using hapi and lab for testing used one of your code ex' and got an error for memory leak.
`
var unirest = require('unirest');
var url = some url goes here...
var request = unirest.get(url);
request.query({
paramA: this.getMetroId({}),

})
.headers({ 'Accept': 'application/json' })
.end(function (response) {
    var error = null;
    if (!response || response === ''){
        error = 'empty response';
    }

    return cb(error, response.body);
});

`

that's the error i got when running my test
The following leaks were detected:Unirest

as you can see i used unirest all lower case after short investigation i found it in your code guys. now I switch to request.js no memory leak found.

I would love to use your node module but there are some issue.

thanks,

  • Doron.

request form-data

In unirest we now have method ".attach(field, file)" and this posts as encapsulated multipart. But I want to send different data as multipart two (raw text, etc.).
I can do this with "request" module, using its ".form().append()" method.
Is it possible to develop smth like it for unirest? I can't find a way to do this in current version...

.auth doesn't work together with .attach

When you attach a file using .attach, .auth seems to be handled wrong.

This code

unirest = require('unirest');

unirest.get('<upload endpoint behind a basic authentication>')
  .auth('test', 'test', true)
  .attach('file', 'uniresttest.js')
  .end(function (response) { 
    console.log(response.status); 
  });

results in:

buffer.js:167
  throw new TypeError('must start with number, buffer, array or string');
  ^

TypeError: must start with number, buffer, array or string
    at fromObject (buffer.js:167:9)
    at new Buffer (buffer.js:58:10)
    at new ClientRequest (_http_client.js:95:20)
    at Object.exports.request (http.js:31:10)
    at FormData.submit (/Users/dennis.ploeger/work/uniresttest/node_modules/unirest/node_modules/form-data/lib/form_data.js:312:20)
    at Object.$this.end (/Users/dennis.ploeger/work/uniresttest/node_modules/unirest/index.js:538:39)
    at Object.<anonymous> (/Users/dennis.ploeger/work/uniresttest/uniresttest.js:3:119)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)

If you manually generate an Authorization header and use .header to inject it in the request, it works.

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.