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

install node-fetch

npm install node-fetch
Comment

PREVIOUS NEXT
Code Example
Javascript :: addeventlistener javascript 
Javascript :: bodyparser express deprecated 
Javascript :: react dom cdn 
Javascript :: usehistory hook 
Javascript :: for each 
Javascript :: delete item from array vuejs 
Javascript :: The document.getElementById() Method 
Javascript :: how to get the all input element id value 
Javascript :: JavaScript for loop Display Sum of n Natural Numbers 
Javascript :: js copy array 
Javascript :: react select disable option 
Javascript :: javascript run function based on the page size 
Javascript :: vue 3 create component 
Javascript :: index of row jquery 
Javascript :: jquery selector id ends with 
Javascript :: node.js anonymous function 
Javascript :: Heroku H10-App Crashed Error 
Javascript :: Shopify.formatMoney 
Javascript :: p5js click on button 
Javascript :: react native navigation shared element 
Javascript :: node js require all function from another file 
Javascript :: parse json c# 
Javascript :: js remove specific item from array 
Javascript :: client.on ready 
Javascript :: how to get duplicate values from array in javascript 
Javascript :: password validation with regular expression in javascript 
Javascript :: using regex in javascript 
Javascript :: javascript do while 
Javascript :: get element with data attribute javascript 
Javascript :: update map value javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =