Git Product home page Git Product logo

node-request-to-curl's Introduction

node-request-to-curl

Adds a .toCurl() method to http.ClientRequests to generate output equivalent to the 'Copy as Curl' option in the WebKit debugger.

Features

  • Based from the WebKit code that Chrome uses in its debugger to generate curl commands
  • Hooks into Node.js at a low-level and is compatible with helper libraries (tested with request)
  • Parses the outgoing request to get what was sent over the wire (compatible with streams)

Use in production

All outgoing HTTP requests are parsed using Node's own HTTP library in a manner similar to how an incoming request is handled. This will incur a minor performance penalty.

Headers

The code from WebKit removes headers that curl calculates for us (namely content-length).

How to use

request

var request = require('request');
require('request-to-curl');

request('http://www.google.com', function (error, response, body) {
    console.log(response.request.req.toCurl());
});

Output:

curl 'http://www.google.com/' --compressed

http

var http = require('http'),
    querystring = require('querystring');

require('request-to-curl');

var postData = querystring.stringify({
    'msg': 'Hello World!'
});

var options = {
    hostname: 'www.google.com',
    port: 80,
    path: '/upload',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': postData.length
    }
};

var req = http.request(options, (res) => {
    console.log(req.toCurl());
});

req.on('error', (e) => {
    console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end();

Output:

curl 'http://www.google.com/upload' -H 'content-type: application/x-www-form-urlencoded' --data 'msg=Hello%20World!' --compressed

Alternatives

request-as-curl is a similar module, however, it does not parse the request over the wire and is not compatible with streams. request-as-curl requires you to pass in your request body (which should be doable in most cases) and write code for each request. Global error handling is complicated by needing to keep the request body in scope.

request-to-curl favors over-the-wire accuracy for all outgoing requests.

request-as-curl does not need to parse outgoing requests over the wire and is ideal if you want to debug a handful of requests and do not use streams. request-to-curl is what you want if you want to be log the curl command to reproduce any failed outgoing HTTP requests anywhere in your app (which is very helpful for working with third party HTTP APIs).

node-request-to-curl's People

Contributors

jmealo avatar tchock avatar

Watchers

James Cloos avatar

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.