fetch-api

Why do fetch errors not have a stacktrace in my single page application?

久未见 提交于 2020-06-29 13:26:33
问题 I have two simple wrappers that handle requests in my single page application. One wraps fetch and throws an error if a response is not ok (not in the 200-300 range): const fetchy = (...args) => fetch(...args).then(response => { if (response.ok) { return response } throw new Error(response.statusText) }) export default fetchy And one wraps fetchy and is used for GET requests: const get = endpoint => { const headers = new Headers({ Authorization: `Bearer ${TOKEN}` }) const init = { method:

How to GET image in reactjs from api?

孤者浪人 提交于 2020-06-26 07:37:19
问题 I am fetching an image from nodejs API after verifying with JWT token. I am getting GET 200 ok response in browser Network header and picture can be seen in Preview, but I cannot use it in my app. I am surely doing something wrong. Please let me know the proper way to display image from API. On my backend nodejs, I am using res.sendFile to send the file. class Card extends Component { constructor({props, pic, token}) { super(props, pic, token); this.state = { pic: pic, }; urlFetch(data) {

How to GET image in reactjs from api?

本小妞迷上赌 提交于 2020-06-26 07:35:12
问题 I am fetching an image from nodejs API after verifying with JWT token. I am getting GET 200 ok response in browser Network header and picture can be seen in Preview, but I cannot use it in my app. I am surely doing something wrong. Please let me know the proper way to display image from API. On my backend nodejs, I am using res.sendFile to send the file. class Card extends Component { constructor({props, pic, token}) { super(props, pic, token); this.state = { pic: pic, }; urlFetch(data) {

Making fetch API work with CORS after OPTIONS response

馋奶兔 提交于 2020-06-24 11:32:15
问题 I are trying to fetch data from our API. The API has enabled CORS support and returns the below response to the OPTIONS request: Access-Control-Request-Headers:content-type Access-Control-Allow-Origin:* The API doesn't allow 'Content-type' anything other than 'application/json' . Using this limitation, I am trying to use the fetch method of React-Native to get the data. Method 1 (no-cors): { method: 'POST', mode: "no-cors", headers: { 'content-type': 'application/json' } With this method, the

How to fetch (using Fetch API) only headers in Javascript, not the whole content?

自古美人都是妖i 提交于 2020-06-08 07:19:39
问题 Here I have this sample fiddle, where I try to get only the content-type header from a large image, using both XMLHttpRequest() and fetch() in Javascript. If you run the fiddle with Chrome's Developer Tools / Network tab (or similar tools in other browsers) open, you'll see that while the XMLHttpRequest() method gets only the 600 B where the headers are located, the fetch() method gets the whole 7.8 MB image, despite my code only needing the content-type headers. How can I get just those few

How to make javascript fetch synchronous? [duplicate]

 ̄綄美尐妖づ 提交于 2020-05-26 10:24:07
问题 This question already has answers here : How do I return the response from an asynchronous call? (39 answers) How to return data from promise [duplicate] (3 answers) How to get response data outside promise chain of Fetch? [duplicate] (1 answer) Closed 2 years ago . I'm using fetch to get data json from an api. Works fine but I have to use it repeatedly for various calls, thus it needs to be synchronous or else I need some way to update the interface when the fetch completes for each

How to chain multiple fetch() promises?

老子叫甜甜 提交于 2020-05-26 09:09:34
问题 The following code fetches a json list and then does another fetch call for each list item to change their values. The problem is that it’s not done synchronously. “new” is printed to the console before “update”. fetch(API_URL_DIARY) .then(response => response.json()) .then(data => { console.log("old", data); return data; }) .then(data => { data.forEach(function(e, index,array) { fetch(API_URL_FOOD_DETAILS + e.foodid) .then(response => response.json()) .then(data => { array[index] = {...e, ..

How to get Header Location value from a Fetch request in browser

浪子不回头ぞ 提交于 2020-05-11 05:35:18
问题 From a ReactJS - Redux front app, I try to get the Location Header value of an REST API response. When I Curl this : curl -i -X POST -H "Authorization: Bearer MYTOKEN" https://url.company.com/api/v1.0/tasks/ I Have this answer : HTTP/1.1 202 ACCEPTED Server: nginx Date: Fri, 12 Aug 2016 15:55:47 GMT Content-Type: text/html; charset=utf-8 Content-Length: 0 Connection: keep-alive Location: https://url.company.com/api/v1.0/tasks/status/idstatus When I make a Fetch in ReactJS var url = 'https:/

How to get Header Location value from a Fetch request in browser

淺唱寂寞╮ 提交于 2020-05-11 05:35:08
问题 From a ReactJS - Redux front app, I try to get the Location Header value of an REST API response. When I Curl this : curl -i -X POST -H "Authorization: Bearer MYTOKEN" https://url.company.com/api/v1.0/tasks/ I Have this answer : HTTP/1.1 202 ACCEPTED Server: nginx Date: Fri, 12 Aug 2016 15:55:47 GMT Content-Type: text/html; charset=utf-8 Content-Length: 0 Connection: keep-alive Location: https://url.company.com/api/v1.0/tasks/status/idstatus When I make a Fetch in ReactJS var url = 'https:/

How can I download and save a file using the Fetch API? (Node.js)

℡╲_俬逩灬. 提交于 2020-05-11 04:25:48
问题 I have the url to a possibly large (100+ Mb) file, how do I save it in a local directory using fetch? I looked around but there don't seem to be a lot of resources/tutorials on how to do this. Thank you! 回答1: Using the Fetch API you could write a function that could download from a URL like this: const downloadFile = (async (url, path) => { const res = await fetch(url); const fileStream = fs.createWriteStream(path); await new Promise((resolve, reject) => { res.body.pipe(fileStream); res.body