Skip to content

node-fetchvshttp.rb

MIT 207 10 8,709
224.9 million (month) Dec 28 2012 3.3.2(7 months ago)
2,994 10 95 NA
Mar 20 2015 447 (month) 0.12.0(6 years ago)

node-fetch is a lightweight library that provides a fetch()-like API for making HTTP requests in Node.js. It is a light-weight implementation of the Fetch API, which is mostly compatible with the browser's version.

node-fetch is primarily known as almost identical package fetch() is included in web browsers so it shares the same use common API. It's great starting point for people coming from front-end environment.

http is an HTTP library for Ruby, it's a fork of the Ruby standard library Net::HTTP. It is designed to provide a more modern and consistent API for making HTTP requests and handling responses.

One of the main goals of http is to simplify the process of making HTTP requests and handling responses. It provides a consistent API for making requests and handling responses across different versions of Ruby and different HTTP libraries, making it easier to write cross-compatible code.

http supports all the standard HTTP methods such as GET, POST, PUT, DELETE, and PATCH, and allows you to set headers, query parameters, and request bodies.

Highlights


popular

Example Use


const fetch = require('node-fetch');

// fetch supports both Promises and async/await
fetch('http://httpbin.org/get')
    .then(res => res.text())
    .then(body => console.log(body))
    .catch(err => console.error(err));
const response = await fetch('http://httpbin.org/get');

// for concurrent scraping Promise.all can be used
const results = await Promise.all([
  fetch('http://httpbin.org/html'),
  fetch('http://httpbin.org/html'),
  fetch('http://httpbin.org/html'),
])

// POST requests
await fetch('http://httpbin.org/post', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name: 'John Doe' }),
})

// Proxy use:
const agent = new https.Agent({
    rejectUnauthorized: false,
    proxy: {
        host: 'proxy.example.com',
        port: 8080
    }
});

await fetch('https://httpbin.org/ip', { agent })

// setting headers and cookies
const headers = new fetch.Headers();
headers.append('Cookie', 'myCookie=123');
headers.append('X-My-Header', 'myValue');

await fetch('https://httpbin.org/headers', { headers })
require 'http'

# GET request
response = HTTP.get("http://httpbin.org/get")
puts response.body
puts response.status
puts response.headers

# POST request
response = HTTP.post("http://httpbin.org/post", json: { title: 'foo', body: 'bar', userId: 1 })
puts response.body

Alternatives / Similar


Was this page helpful?