fetch-api

Re-calling useEffect after a failed api fetching request

谁说胖子不能爱 提交于 2020-04-17 20:38:13
问题 I am executing useEffect() to update a state with JSON data. However the fetch request sometimes fails, so I want to re-execute the useEffect hook if that happens: ... import React, {useState, useEffect} from 'react'; import {getJsonData} from './getJsonData'; const myApp = () => { var ErrorFetchedChecker = false; const [isLoading,setIsLoading] = useState(true); const [data,setData] = useState(null); const updateState = jsonData => { setIsloading(false); setData(jsonData); }; useEffect(() =>

making two fetch requests in one function with a delay in between

ε祈祈猫儿з 提交于 2020-04-17 03:35:53
问题 so I have 2 endpoints, I'm making a "post" request to the first one and in the response I should get some kind of id that I use in the second request endpoint url. so I want to make a delay or something till I get the response from the first request. req = () => { fetch('url', { method: 'POST', headers: { 'Authorization': bearer, 'Content-Type': 'application/json', }, body: }) .then(response => response.json()) .then(json => { }) fetch(`url with the id I get from the first request`, { method:

making two fetch requests in one function with a delay in between

喜欢而已 提交于 2020-04-17 03:35:31
问题 so I have 2 endpoints, I'm making a "post" request to the first one and in the response I should get some kind of id that I use in the second request endpoint url. so I want to make a delay or something till I get the response from the first request. req = () => { fetch('url', { method: 'POST', headers: { 'Authorization': bearer, 'Content-Type': 'application/json', }, body: }) .then(response => response.json()) .then(json => { }) fetch(`url with the id I get from the first request`, { method:

Using Fetch API to POST XML

。_饼干妹妹 提交于 2020-04-13 05:09:22
问题 I'm trying to use Fetch API to handle POST of XML data to avoid cross-origin issues with XmlHttpRequest. The issue I face is that despite setting my Content-Type to 'text/xml' (which is the only supported content-type header in this case) my request's content-type is being reset to text/plain resulting in a HTTP status of 415 from the requested content. Here is my fetch function: function doFetch(Content) { return fetch( URL, { method: 'POST', mode: 'no-cors', headers: new Headers( {'Content

Using Fetch API to POST XML

不羁岁月 提交于 2020-04-13 05:09:06
问题 I'm trying to use Fetch API to handle POST of XML data to avoid cross-origin issues with XmlHttpRequest. The issue I face is that despite setting my Content-Type to 'text/xml' (which is the only supported content-type header in this case) my request's content-type is being reset to text/plain resulting in a HTTP status of 415 from the requested content. Here is my fetch function: function doFetch(Content) { return fetch( URL, { method: 'POST', mode: 'no-cors', headers: new Headers( {'Content

CORS error, but data is fetched regardless

流过昼夜 提交于 2020-04-13 04:14:39
问题 I have a generated React site I am hosting in an S3 bucket. One of my components attempts to fetch something when loaded: require('isomorphic-fetch') ... componentDidMount() { fetch(`${url}`) .then(res => { console.log(res); this.setState({ users: res }) }) .catch(e => { // do nothing }) } The url I am fetching is an AWS API Gateway. I have enabled CORS there, via the dropdown, with no changes to the default configuration. In my console, for both the remote site and locally during development

How to get the content-length of the response from a request with fetch()

生来就可爱ヽ(ⅴ<●) 提交于 2020-04-08 09:25:10
问题 I was getting an error when returning response.json() when I would do a request with an empty response body, so I'm trying to just return an empty object when there is an empty body. The approach I was going for is to check the Content-Length header of the response, however, somehow response.headers.get('Content-Length') somehow returns null . Here is my code: function fetchJSON(url, options, state = null) { return fetch(url, Object.assign({}, options, { // TODO: Add options here that should

How to get the content-length of the response from a request with fetch()

不打扰是莪最后的温柔 提交于 2020-04-08 09:15:04
问题 I was getting an error when returning response.json() when I would do a request with an empty response body, so I'm trying to just return an empty object when there is an empty body. The approach I was going for is to check the Content-Length header of the response, however, somehow response.headers.get('Content-Length') somehow returns null . Here is my code: function fetchJSON(url, options, state = null) { return fetch(url, Object.assign({}, options, { // TODO: Add options here that should

Why is the response body empty (0 bytes on network tab) for this request? Is it to do with this being an extension?

心不动则不痛 提交于 2020-03-26 03:58:42
问题 When I use the fetch API (Or xmlhttprequest) I get a 0 byte response. Here is a sample of my code: fetch("https://myurl", { method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({content: content}) }).then(function(res){ return res.text()}).then(function(res){ return cb(res);}); In the network tab, and in the console.log(res) in the callback, the response is empty. I should note that the response is including a CORS response

What happens when I execute several fetch at the same time?

无人久伴 提交于 2020-03-05 01:37:28
问题 What happens when I execute several fetch at the same time? Does each fetch run completely separately and one fetch doesn't have to wait for another to finish? Or are all the fetch somehow "linked"? Let us take a concrete example: - I execute two fetch at the same time, one that calls an API and asks to retrieve information and the other one that calls another API but this one has a 'long response time' (so the fetch will run for a long time). Can the second fetch influence the response time