fetch-api

Read the body of a Fetch Promise

倖福魔咒の 提交于 2019-12-02 21:48:51
I'm sure this has a simple answer, but for the life of me I can't figure out how to do it. I have the following express endpoint for uploading to Google Cloud storage. It works great and the response from the google api gives me a unique file name that I want to pass back to my front end: app.post('/upload', (req, res) => { var form = new formidable.IncomingForm(), files = [], fields = []; form .on('field', function(field, value) { fields.push([field, value]); }) .on('file', function(field, file) { files.push([field, file]); }) .on('end', function() { console.log('-> upload done'); }); form

How to set the content-type of request header when using Fetch APi

我只是一个虾纸丫 提交于 2019-12-02 21:39:38
I am using npm 'isomorphic-fetch' to send requests. The problem I am experiencing is I am unable to set the content-type of the request header. I set a content type of application/json , however the request header are being set to text/plain. import 'isomorphic-fetch'; sendRequest(url, method, body) { const options = { method: method, headers:{'content-type': 'application/json'}, mode: 'no-cors' }; options.body = JSON.stringify(body); return fetch(url, options); } When I examine the request in my browser the content type is o : content-type:text/plain;charset=UTF-8 Can anyone explain why I am

fetch not defined in Safari (ReferenceError: Can't find variable: fetch)

女生的网名这么多〃 提交于 2019-12-02 18:13:34
For some reason fetch ( https://fetch.spec.whatwg.org/ ) is not defined in Safari (Version 9.0.3), does anyone know why? It seems to be the standard and works fine in Chrome and Firefox. Can't seem to find anyone else having the same issue I am using react with redux and here is some example code: export function fetchData (url) { return dispatch => { dispatch(loading()) fetch(url, { method: 'GET' }) .then(response => { response.json() .then(data => { dispatch(success(data)) }) }) } } Dmitriy You can use https://github.com/github/fetch polyfill for unsupported browsers. npm install whatwg

Fetch vs. AjaxCall

…衆ロ難τιáo~ 提交于 2019-12-02 14:39:17
What is the difference between typical AJAX and Fetch API? Consider this scenario: function ajaxCall(url) { return new Promise(function(resolve, reject) { var req = new XMLHttpRequest(); req.open('GET', url); req.onload = function() { if (req.status == 200) { resolve(req.response); } else { reject(Error(req.statusText)); } }; req.onerror = function() { reject(Error("Network Error")); }; req.send(); }); } ajaxCall('www.testSite').then(x => { console.log(x) }) // returns html of site fetch('www.testSite').then(x => { console.log(x) }) // returns object with information about call This is what

Fetch api - getting json body in both then and catch blocks for separate status codes

流过昼夜 提交于 2019-12-02 13:37:13
问题 I am using fetch api for fetching an URL that might return: Response : status = 200, json body = {'user': 'abc', 'id': 1} or Response : status = 400 , json body = {'reason': 'some reason'} or Response : status = 400 , json body = {'reason': 'some other reason'} I want to make a separate function request() that I use from various parts of my code as follows: request('http://api.example.com/').then( // status 200 comes here data => // do something with data.id, data.user ).catch( // status 400,

Error when accessing API with fetch while setting mode to 'no-cors' [duplicate]

浪子不回头ぞ 提交于 2019-12-02 11:51:50
This question already has an answer here: Handle response - SyntaxError: Unexpected end of input when using mode: 'no-cors' 5 answers When trying to resolve a fetch promise with JS is set the mode to 'no-core' based on this answer . However setting the mode to 'corse' results in having: Access to fetch at '{endpoint}' from origin ' http://localhost:3000 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. So I wrote

using of fetch API the unexpected end of input error occurr

一个人想着一个人 提交于 2019-12-02 09:54:27
fetch('http://www.freegamesforyourwebsite.com/feeds/games/? tag=platform&limit=100&format=json', { method:'GET', mode:'no-cors', dataType: 'json', headers: { 'Accept': 'application/json' } }).then(response => response.json()).then(addImage).catch(e => requestError(e,'image')); console.log SyntaxError: Unexpected end of input at fetch.then.response ((index):159) at <anonymous> I try using the fetch the json with cros platform, why the request header in Google developer tool shows me the provision request header.And the preview part can show me the json response, but the promise after the

Fetch api - getting json body in both then and catch blocks for separate status codes

落花浮王杯 提交于 2019-12-02 05:17:25
I am using fetch api for fetching an URL that might return: Response : status = 200, json body = {'user': 'abc', 'id': 1} or Response : status = 400 , json body = {'reason': 'some reason'} or Response : status = 400 , json body = {'reason': 'some other reason'} I want to make a separate function request() that I use from various parts of my code as follows: request('http://api.example.com/').then( // status 200 comes here data => // do something with data.id, data.user ).catch( // status 400, 500 comes here error => // here error.reason will give me further info, i also want to know whether

ReactJS: What is the correct way to set a state value as array?

天涯浪子 提交于 2019-12-02 04:49:21
问题 I have an array of object that get users data using fetch API. I have tried constructor, create a function, bind it. It didn't work. I tried ComponentDidMount and setState, it returns undefined. class Admin extends Component { componentDidMount () { var that = this; fetch('http://localhost:4500/data/users', { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', } }).then(function(response) { return response.json(); }).then(function(json){ console.log

IBM Watson Conversation API: “Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response”

我只是一个虾纸丫 提交于 2019-12-02 04:04:15
I created a React-Native app that connected to the Watson REST APIs. Using the fetch library that is part of the ReactNative, everything was working well for getting the Workspaces list, like this: const myAuth = new Buffer(USR+':'+PWD).toString('base64'); const myInit = { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Origin': '', 'Authorization': 'Basic ' + myAuth, } }; return fetch(URL, myInit) .then((response) => response.json()) .then((responseJson) => { ... } I am now moving to React (rather than ReactNative) and the whatwg-fetch library. The