fetch-api

Can you set the Host header using fetch API

流过昼夜 提交于 2019-12-04 04:28:08
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 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-you-set-the-host-header-using-fetch-api

Fetch API post XML request error

六月ゝ 毕业季﹏ 提交于 2019-12-04 02:13:08
问题 I am failing to do a fetch POST request. It returns a 400 bad request error fetch('http://192.168.1.6:49152/ctl/RenderingControl', { method: 'POST', headers: { "SOAPAction": "urn:schemas-upnp-org:service:RenderingControl:1#GetVolume", "content-type": "text/xml; charset=utf-8" }, body: '<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body><u:GetVolume xmlns:u="urn:schemas-upnp-org:service

POST Request with Fetch API?

别等时光非礼了梦想. 提交于 2019-12-03 18:36:10
问题 I know that with the new Fetch API (used here with ES2017's async / await ) you can make a GET request like this: async getData() { try { let response = await fetch('https://example.com/api'); let responseJson = await response.json(); console.log(responseJson); } catch(error) { console.error(error); } } But how do you make a POST request? 回答1: Long story short, Fetch also allows you to pass an object for a more personalized request: fetch("http://example.com/api/endpoint/", { method: "post",

How do I fix CORS issue in Fetch API

你离开我真会死。 提交于 2019-12-03 15:14:47
I'm building a front-end only basic Weather App using reactjs. For API requests I'm using Fetch API. In my app, I'm getting the current location from a simple API I found and it gives the location as a JSON object. But when I request it through Fetch API, I'm getting this error. Failed to load http://ip-api.com/json: Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response. So I searched through and found multiple solutions to fix this. Enabling CORS in Chrome solves the error but when I deploy the app on heroku, how can I access it

How to get headers of the response from fetch

北战南征 提交于 2019-12-03 11:00:44
I am using fetch on chrome Version 52.0.2743.82 (64-bit). I want to get all the headers in the response. Following snippet only return content-type but if you peek into chrome dev tools it shows many other response headers. How to get other headers from fetch. fetch('https://httpbin.org/get') .then(response => { const headers = response.headers.entries(); let header = headers.next(); while (!header.done){ console.log(headers.value); header = header.next(); } }) I tried polyfilling(manually overriding) the github implementation. Still no luck. Adnan Umer You can't access all the headers when

React Fetch not working in IE11

删除回忆录丶 提交于 2019-12-03 10:29:46
I have a ReactJS application that works as expected in Chrome, but fails in IE-11. The problem is this - we have two drop down lists which are populated from rest services when the page is first loaded. The application is running under SSL. When the page is loaded through IE-11, I get an IE-11 bug issue where the first request call gets cancelled out by the second-the bug is described here: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/1282036/ So, I am just asking the community whether there is a work around for IE-11 or is there away to implement my code sequentially

Read the body of a Fetch Promise

六月ゝ 毕业季﹏ 提交于 2019-12-03 08:08:52
问题 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,

Javascript Fetch API - How to save output to variable as an Object (not the Promise)

烈酒焚心 提交于 2019-12-03 07:50:44
问题 Please, how can I save output of fetch to a variable - to be able to work with it as with an object? Here is the code: var obj; fetch("url", { method: "POST", body: JSON.stringify({ "filterParameters": { "id": 12345678 } }), headers: {"content-type": "application/json"}, //credentials: 'include' }) .then(res => res.json()) .then(console.log) The final console.log will show an object. But when I tried to save it to variable .then(res => obj = res.json()) than the console.log(obj) will not hold

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

£可爱£侵袭症+ 提交于 2019-12-03 06:28:36
问题 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

Fetch vs. AjaxCall

拈花ヽ惹草 提交于 2019-12-03 01:20:03
问题 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