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 :: console log in vue 
Javascript :: jquery await async 
Javascript :: all ajaxcomplete event 
Javascript :: javascript object to base64 
Javascript :: js filter blur Property 
Javascript :: install react router dom 
Javascript :: Best Way to reset form elements 
Javascript :: global error handling middleware express 
Javascript :: error while connecting mongodb MongoParseError: option usefindandmodify is not supported 
Javascript :: javascript caesar cipher 
Javascript :: node js send fcm 
Javascript :: Axios FormData / not JSON 
Javascript :: js check if string is number 
Javascript :: simple reactjs login form 
Javascript :: to add autofix when saving files in Eslint 
Javascript :: how to check the extension of a file in javascript 
Javascript :: reverse a number in javascript w3schools 
Javascript :: jquery sort listing alphabetically 
Javascript :: javascript name capitalization 
Javascript :: js generate random color 
Javascript :: javascrpt formatBytes 
Javascript :: jquery select element with two classes 
Javascript :: express-generator 
Javascript :: javascript array functions 
Javascript :: mongodb aggregate skip results 
Javascript :: js styles when clicked 
Javascript :: convert long date to short date javascript 
Javascript :: Without using a new array or the reverse() method to Reverse an Array 
Javascript :: javascript onsubmit 
Javascript :: TypeError: this.authenticate is not a function 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =