fetch-api

Javascript Fetch API: header params not working

陌路散爱 提交于 2019-12-10 15:35:33
问题 This is my sample request: var header = new Headers({ 'Platform-Version': 1, 'App-Version': 1, 'Platform': 'FrontEnd' }); var myInit = { method : 'GET', headers: header, mode : 'no-cors', cache : 'default' } fetch('http://localhost:3000/api/front_end/v1/login', myInit) .then(res => { console.log(res.text()) }) When I debug, I see that this request is sent successfully to server, but server hasn't received header params (in this case is Platform-Version , App-Version and Platform ). Please

React-Native fetch through proxy

给你一囗甜甜゛ 提交于 2019-12-10 12:35:03
问题 How can I set options in the fetch method of react-native to make a call to a webserver go through a proxy. Currently I am using axios but this doesn't work. After adding a host header to the fetch, I can make a request through charles-proxy but it doesn't work with squid. ( Error: invalid url ). 回答1: Hello You can use this module node-fetch(https://github.com/bitinn/node-fetch) and set agent param for proxy, For more details, you can also check this https://github.com/bitinn/node-fetch

Fetch API default cross-origin behavior

筅森魡賤 提交于 2019-12-10 10:43:57
问题 The Fetch Specifications say that the default Fetch mode is 'no-cors' - A request has an associated mode, which is "same-origin", "cors", "no-cors", "navigate", or "websocket". Unless stated otherwise, it is "no-cors". But, I seem to be noticing this behavioral difference between mode: 'no-cors' and an unspecified mode. As demonstrated in this JSFiddle sample, explicitly defining mode as 'no-cors' makes the response inaccessible to the Javascript, while not specifying a mode makes the

Request cannot be constructed from a URL that includes credentials

穿精又带淫゛_ 提交于 2019-12-10 10:38:57
问题 I want to get a JSON from an API in React.js. I'm tried with axios, superagent and fetch but it's doesn't worked? let token = '****'; let url = 'https://'+ token +'@api.navitia.io/v1/coverage/fr-idf/stop_areas/stop_area%3AOIF%3ASA%3A59491/departures?'; let myInit = { 'method': 'GET' } fetch(url, myInit).then((response)=>{ return response.json(); }).then((data)=> { console.log('ok'); }).catch(function(err){ console.log('Erreur: ' + err); }); Error: "Request cannot be constructed from a URL

React Native - mocking FormData in unit tests

百般思念 提交于 2019-12-10 03:39:52
问题 I'm having issues testing my thunks, as many of my API calls are using FormData, and I can't seem to figure out how to mock this in tests. I'm using Jest. My setup file looks like this: import 'isomorphic-fetch'; // Mocking the global.fetch included in React Native global.fetch = jest.fn(); // Helper to mock a success response (only once) fetch.mockResponseSuccess = body => { fetch.mockImplementationOnce(() => Promise.resolve({ json: () => Promise.resolve(JSON.parse(body)) }) ); }; // Helper

How do I copy a Request object with a different URL?

房东的猫 提交于 2019-12-10 02:21:32
问题 I'm writing a wrapper around fetch that I would like to add something to the URL before making the request e.g. identifying query parameters. I can't figure out how to make a copy of a given a Request object with a different URL than the original. My code looks like: // My function which tries to modify the URL of the request function addLangParameter(request) { const newUrl = request.url + "?lang=" + lang; return new Request(newUrl, /* not sure what to put here */); } // My fetch wrapper

ReactJS componentDidMount and Fetch API

 ̄綄美尐妖づ 提交于 2019-12-09 18:58:27
问题 Just started using ReactJS and JS, is there a way to return the JSON obtained from APIHelper.js to setState dairyList in App.jsx? I think I'm not understanding something fundamental about React or JS or both. The dairyList state is never defined in Facebook React Dev Tools. // App.jsx export default React.createClass({ getInitialState: function() { return { diaryList: [] }; }, componentDidMount() { this.setState({ dairyList: APIHelper.fetchFood('Dairy'), // want this to have the JSON }) },

Can you set the Host header using fetch API

被刻印的时光 ゝ 提交于 2019-12-09 16:32:28
问题 I have a reverse proxy server, which redirects you to different services depending on the Host header. However when making requests to this server using a browser, the Host is always set to the domain name in the URL. I tried: fetch("http://foo.com", {"headers":{"Host":"bar.foo.com"}}) But it doesn't work 回答1: Host is one of the forbidden header names: A forbidden header name is an HTTP header name that cannot be modified programmatically. 来源: https://stackoverflow.com/questions/43285286/can

Get the data from fetch -> promise -> response

此生再无相见时 提交于 2019-12-09 10:38:13
问题 I am trying to post some data to the server but I don't know how to get back the response data. I have the following code: fetch(url, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ email: login, password: password, }) }).then(function(a){ console.log(a); }) It prints a Response it contains data such as body (ReadableByteStream), bodyUsed (false), ok (true), status (200),... but I cannot find the data I get back, nowhere.

Trying to access response data using fetch

青春壹個敷衍的年華 提交于 2019-12-09 09:45:07
问题 I'm trying something simple where I make a request from the front end of my app using the fetch API like so let request = new Request('http://localhost:3000/add', { headers: new Headers({ 'Content-Type': 'text/json' }), method: 'GET' }); fetch(request).then((response) => { console.log(response); }); I am handling this request on the server like so, app.get('/add', (req, res) => { const data = { "number1": "2", "number2": "4" }; res.send(data); }); However, when I try to access my data on the