Questions tagged [fetch-api]

The Fetch API is an improved replacement for XHR, for making asynchronous HTTP requests while better managing redirects and interaction with CORS and Service Workers.

The core of the Fetch API is a JavaScript method simply called fetch, for making asynchronous HTTP requests and for handling the responses from those requests.

For more information about the Fetch API, see:

5530 questions
1210
votes
31 answers

No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API

I'm trying to fetch some data from the REST API of HP Alm. It works pretty well with a small curl script—I get my data. Now doing that with JavaScript, fetch and ES6 (more or less) seems to be a bigger issue. I keep getting this error…
daniel.lozynski
  • 14,907
  • 6
  • 18
  • 21
1036
votes
18 answers

Fetch: POST JSON data

I'm trying to POST a JSON object using fetch. From what I can understand, I need to attach a stringified object to the body of the request, e.g.: fetch("/echo/json/", { headers: { 'Accept': 'application/json', 'Content-Type':…
Razor
  • 27,418
  • 8
  • 53
  • 76
463
votes
12 answers

Trying to use fetch and pass in mode: no-cors

I can hit this endpoint, http://catfacts-api.appspot.com/api/facts?number=99 via Postman and it returns JSON Additionally I am using create-react-app and would like to avoid setting up any server config. In my client code I am trying to use fetch to…
dwww
  • 4,888
  • 3
  • 13
  • 12
444
votes
14 answers

Setting query string using Fetch GET request

I'm trying to use the new Fetch API: I am making a GET request like this: var request = new Request({ url: 'http://myapi.com/orders', method: 'GET' }); fetch(request); However, I'm unsure how to add a query string to the GET request. Ideally,…
mylescc
  • 5,720
  • 3
  • 17
  • 23
360
votes
11 answers

Retrieve data from a ReadableStream object?

How may I get information from a ReadableStream object? I am using the Fetch API and I don't see this to be clear from the documentation. The body is being returned as a ReadableStream and I would simply like to access a property within this stream.…
noob
  • 5,954
  • 6
  • 20
  • 32
350
votes
11 answers

How do I post form data with fetch api?

My code: fetch("api/xxx", { body: new FormData(document.getElementById("form")), headers: { "Content-Type": "application/x-www-form-urlencoded", // "Content-Type": "multipart/form-data", }, method: "post", } I tried…
Zack
  • 3,799
  • 2
  • 11
  • 12
344
votes
17 answers

How do I POST a x-www-form-urlencoded request using Fetch?

I have some parameters that I want to POST form-encoded to my server: { 'userName': 'test@gmail.com', 'password': 'Password!', 'grant_type': 'password' } I'm sending my request (currently without parameters) like this var obj = { …
texas697
  • 5,609
  • 16
  • 65
  • 131
334
votes
10 answers

Fetch API with Cookie

I am trying out the new Fetch API but is having trouble with Cookies. Specifically, after a successful login, there is a Cookie header in future requests, but Fetch seems to ignore that headers, and all my requests made with Fetch is…
Khanetor
  • 11,595
  • 8
  • 40
  • 76
332
votes
12 answers

What is difference between Axios and Fetch?

I am calling the web service by using Fetch but the same I can do with the help of Axios. So now I am confused. Should I go for either Axios or Fetch?
Gorakh Nath
  • 9,140
  • 15
  • 46
  • 68
320
votes
11 answers

How do I upload a file with the JS fetch API?

I am still trying to wrap my head around it. I can have the user select the file (or even multiple) with the file input:
deitch
  • 14,019
  • 14
  • 68
  • 96
297
votes
7 answers

How do I cancel an HTTP fetch() request?

There is a new API for making requests from JavaScript: fetch(). Is there any built in mechanism for canceling these requests in-flight?
Sam Lee
  • 9,913
  • 15
  • 48
  • 56
278
votes
3 answers

Fetch API vs XMLHttpRequest

I know that Fetch API uses Promises and both of them allow you to do AJAX requests to a server. I have read that Fetch API has some extra features, which aren't available in XMLHttpRequest (and in the Fetch API polyfill, since it's based on…
ilyabasiuk
  • 4,270
  • 4
  • 22
  • 35
258
votes
9 answers

Basic authentication with fetch?

I want to write a simple basic authentication with fetch, but I keep getting a 401 error. It would be awesome if someone tells me what's wrong with the code: let base64 = require('base-64'); let url =…
daniel.lozynski
  • 14,907
  • 6
  • 18
  • 21
241
votes
14 answers

Fetch API request timeout?

I have a fetch-api POST request: fetch(url, { method: 'POST', body: formData, credentials: 'include' }) I want to know what is the default timeout for this? and how can we set it to a particular value like 3 seconds or indefinite seconds?
Akshay Lokur
  • 6,680
  • 13
  • 43
  • 62
236
votes
6 answers

Why does .json() return a promise?

I've been messing around with the fetch() api recently, and noticed something which was a bit quirky. let url = "http://jsonplaceholder.typicode.com/posts/6"; let iterator = fetch(url); iterator .then(response => { return { …
haveacigaro
  • 2,509
  • 2
  • 13
  • 8
1
2 3
99 100