Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node js fetch

const fetch = require('node-fetch');	//npm install node-fetch

fetch('https://httpbin.org/post', {
  method: 'POST',
  body: 'a=1'
})
  .then(res => res.json())
  .then(json => {
	// Do something...
  })
  .catch(err => console.log(err));
Comment

node-fetch

import fetch from 'node-fetch';

const body = {a: 1};

const response = await fetch('https://httpbin.org/post', {
	method: 'post',
	body: JSON.stringify(body),
	headers: {'Content-Type': 'application/json'}
});
const data = await response.json();

console.log(data);
Comment

nodejs fetch

import fetch from 'node-fetch';

const response = await fetch('https://github.com/');
const body = await response.text();

console.log(body);
Comment

node-fetch

//Plain text or HTML
fetch('https://github.com/').then(res => res.text()).then(body => console.log(body));

//JSON
fetch('https://api.github.com/users/github')
    .then(res => res.json())
    .then(json => console.log(json));

//Simple Post
fetch('https://httpbin.org/post', { method: 'POST', body: 'a=1' })
    .then(res => res.json()) // expecting a json response
    .then(json => console.log(json));

//Post with JSON
const body = { a: 1 };
 
fetch('https://httpbin.org/post', {
        method: 'post',
        body:    JSON.stringify(body),
        headers: { 'Content-Type': 'application/json' },
    })
    .then(res => res.json())
    .then(json => console.log(json));
//Post with form parameters

const { URLSearchParams } = require('url');
 
const params = new URLSearchParams();
params.append('a', 1);
 
fetch('https://httpbin.org/post', { method: 'POST', body: params })
    .then(res => res.json())
    .then(json => console.log(json));
Comment

node fetch

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

PREVIOUS NEXT
Code Example
Javascript :: template literals 
Javascript :: jquery slider 
Javascript :: autocannon 
Javascript :: lodash filter array objects 
Javascript :: palindrome 
Javascript :: sort array based on multiple columns javascript 
Javascript :: angular npx 
Javascript :: angular input date pattern validation 
Javascript :: javascript remove event listener after bind 
Javascript :: query parameters 
Javascript :: how to check if an array contains a number in javascript 
Javascript :: js how to have an onclick inside of another onClick 
Javascript :: check if an input element has focus 
Javascript :: javascript find unique values in array of objects 
Javascript :: create function in javascript 
Javascript :: convert a signed 64.64 bit fixed point number online 
Javascript :: how to highlight active screen react native 
Javascript :: rngesturehandlermodule.default.direction react native 
Javascript :: find second smallest number in array javascript using for loop 
Javascript :: pwa cache viewer 
Javascript :: Rounding off to desired no of digit after decimal 
Javascript :: lunix increae ram available to nodejs 
Javascript :: interpolation in js 
Javascript :: Material-ui wallet icon 
Javascript :: jsconfig.json code to support absolute import 
Javascript :: while loop vs for loop javascript 
Javascript :: react svg 
Javascript :: node redirect 
Javascript :: how to trim the file name when length more than 10 in angular 
Javascript :: ValueError: dictionary update sequence element #0 has length 1; 2 is required 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =