fetch-api

Fetch API with Cookie

故事扮演 提交于 2019-11-26 15:00:59
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 unauthorized. Is it because Fetch is still not ready or Fetch does not work with Cookies? I build my app with Webpack. I also use Fetch in React Native, which does not have the same issue. Khanetor Fetch does not use cookie by default. To enable cookie, do this: fetch(url, { credentials: "same-origin" }).then(...).catch(...); In addition to @Khanetor's answer, for

Why does Fetch API Send the first PUT request as OPTIONS

不想你离开。 提交于 2019-11-26 14:52:51
问题 I am trying to make a cors PUT request using the vanilla Fetch API When I click my button to send the PUT request, the method on the first request is OPTIONS . It is only when I click the button again, then the method on the request changes to PUT . Why? I understand this is part of the CORS preflight, but is there a way to trigger the preflight manually so the OPTIONS response can be cached? Could this behavior be indicative of a failing promise somewhere? 回答1: See the Fetch Standard,

Fetch API request timeout?

最后都变了- 提交于 2019-11-26 13:05:00
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? Bruce Lee It doesn't have a specified default; the specification doesn't discuss timeouts at all. You can implement your own timeout wrapper for promises in general: // Rough implementation. Untested. function timeout(ms, promise) { return new Promise(function(resolve, reject) { setTimeout(function() { reject(new Error("timeout")) }, ms) promise.then(resolve,

Fetch request to local file not working

老子叫甜甜 提交于 2019-11-26 11:29:41
问题 I\'m trying to make a request in a local file, but I don\'t know when I try to do on my computer show me an error. Is possible make a fetch to a file inside your project? // Option 1 componentDidMount() { fetch(\'./movies.json\') .then(res => res.json()) .then((data) => { console.log(data) }); } error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 at App.js: 10 --> .then(res => res.json()) // Option 2 componentDidMount() { fetch(\'./movies.json\', { headers : { \

How do I cancel an HTTP fetch() request?

夙愿已清 提交于 2019-11-26 11:09:34
There is a new API for making requests from JavaScript: fetch(). Is there any built in mechanism for canceling these requests in-flight? SudoPlz TL/DR: fetch now supports a signal parameter as of 20 September 2017, but not all browsers seem support this atm . This is a change we will be seeing very soon though, and so you should be able to cancel a request by using an AbortController s AbortSignal . Long Version How to: The way it works is this: Step 1 : You create an AbortController (For now I just used this ) const controller = new AbortController() Step 2 : You get the AbortController s

What limitations apply to opaque responses?

…衆ロ難τιáo~ 提交于 2019-11-26 10:28:55
Opaque responses are defined as part of the Fetch API , and represent the result of a request made to a remote origin when CORS is not enabled. What practical limitations and "gotchas" exist around how opaque responses can be used, both from JavaScript and as resources on a page? Access to Opaque Responses' Headers / Body The most straightforward limitation around opaque responses is that you cannot get meaningful information back from most of the properties of the Response class, like headers , or call the various methods that make up the Body interface, like json() or text() . This is in

Basic authentication with fetch?

夙愿已清 提交于 2019-11-26 10:25:50
问题 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 = \'http://eu.httpbin.org/basic-auth/user/passwd\'; let username = \'user\'; let password = \'passwd\'; let headers = new Headers(); //headers.append(\'Content-Type\', \'text/json\'); headers.append(\'Authorization\', \'Basic \' + base64.encode(username + \":\" + password)); fetch(url, {method:

Using an authorization header with Fetch in React Native

不打扰是莪最后的温柔 提交于 2019-11-26 08:47:01
问题 I\'m trying to use fetch in React Native to grab information from the Product Hunt API. I\'ve obtained the proper Access Token and have saved it to State, but don\'t seem to be able to pass it along within the Authorization header for a GET request. Here\'s what I have so far: var Products = React.createClass({ getInitialState: function() { return { clientToken: false, loaded: false } }, componentWillMount: function () { fetch(api.token.link, api.token.object) .then((response) => response

Fetch with ReadableStream as Request Body

眉间皱痕 提交于 2019-11-26 07:37:25
问题 I\'m trying to use fetch with a ReadableStream. In this example, the ReadableStream should simply repeat \"Some data...\" indefinitely. fetch(\'/\', { method: \'POST\', body: new ReadableStream({ pull: function(controller) { console.log(\'pull called!\'); controller.enqueue(\'Some data...\'); } }) }); This doesn\'t work. While pull is executed once, no data is sent in the request body. POST / HTTP/1.1 Host: example.com Connection: keep-alive Content-Length: 0 Origin: https://example.com User

fetch post with multipart form data

巧了我就是萌 提交于 2019-11-26 06:59:51
问题 I am fetching a URL like this: fetch(url, { mode: \'no-cors\', method: method || null, headers: { \'Accept\': \'application/json, application/xml, text/plain, text/html, *.*\', \'Content-Type\': \'multipart/form-data\' }, body: JSON.stringify(data) || null, }).then(function(response) { console.log(response.status) console.log(\"response\"); console.log(response) }) My API expects the data to be of multipart/form-data so I am using content-type of this type... But it is giving me a response