“nœud js récupérer” Réponses codées

nœud js récupérer

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));
garzj

Nodejs récupérer

import fetch from 'node-fetch';

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

console.log(body);
EnzoK

nœud

//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));
abdelghanyMh

nœud

// mod.cjs
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
Pleasant Puma

Réponses similaires à “nœud js récupérer”

Questions similaires à “nœud js récupérer”

Plus de réponses similaires à “nœud js récupérer” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code