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 :: round till 2 digit in jquery 
Javascript :: js get viewport width 
Javascript :: window.onload 
Javascript :: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. 
Javascript :: javascript voice reader 
Javascript :: js find lowest number in array 
Javascript :: document.ready 
Javascript :: react clear form after save 
Javascript :: json server sorting 
Javascript :: header ejs 
Javascript :: js response to json log 
Javascript :: how to find prime numbers in an array in javascript 
Javascript :: how to add a right click listener javascript 
Javascript :: This version of CLI is only compatible with Angular versions ^9.0.0-beta || =9.0.0 <10.0.0, but Angular version 8.1.3 was found instead. 
Javascript :: get text inside element javascript 
Javascript :: js method string remove extra spaces 
Javascript :: jquery ajax post form 
Javascript :: remove last 3 characters from string javascript 
Javascript :: scoll a div to bottom in angular 
Javascript :: console log all events 
Javascript :: nodemailer gmail example 
Javascript :: get data in date filter sequelize 
Javascript :: readonly attribute in html by javascript 
Javascript :: Node.js: printing to console without a trailing newline 
Javascript :: before in material ui style 
Javascript :: if document is loaded 
Javascript :: strip html tags javascript 
Javascript :: react-native-paper password input 
Javascript :: ReferenceError: fetch is not defined 
Javascript :: remove div javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =