why Axios send two request - javascript

I need help with axios, I don't know why is sending two request here an image of chrome network in one post call
I'm using react, and when I send request the function only trigger once (I debugged and put console.logs and get one response) but in chrome network I got two request, with different headers and type.
One have Authorization token, and the other don't have tokensuccess request and
wrong request this is in one call and I don't know what is happening.
Thanks for your time!

Is it an OPTION request?. OPTION requests are used to check if your client has permission to make the desired request to the API.
The HTTP OPTIONS method requests permitted communication options for a given URL or server. A client can specify a URL with this method, or an asterisk (*) to refer to the entire server.

Related

Is there a way to know if a GraphQL request was sent raw or through the Playground?

We are doing some header checking in our Apollo GraphQL application and noticed that when the user sends a request to initially open Playground in the browser, there are no headers present in this request.
The problem is that in our application, all requests are supposed to contain at least some required headers. If these headers are missing, our code flags these requests and determines the request is not genuine. Therefore, a request to open Playground is flagged because it is missing these required headers.
Is there any way to distinguish whether the incoming request is requesting Playground versus a request that is actually making a call for some data? Is there an attribute somewhere?

spurious ajax OPTIONS request being made along with a GET request

I am making a standard ajax request with the code below
let x = new XMLHttpRequest();
x.onload = function(event) { …};
x.open("GET", url, true);
x.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
x.send();
The problem is that, for some reason I can't fathom, two ajax requests are made. The first one is an OPTIONS request and the second one is the GET request that I actually wanted. Any idea why?
My server is a nodejs app which I am starting using nodemon which restarts the server if it senses index.js has changed (this is helpful in development when you don't want to stop and restart the server). With this OPTIONS business, nodemon thinks my index.js has changed and restarts nodejs.
Additionally, the OPTIONS request results in a successful request that returns http 200. But, nothing is returned to the web page that initiated that ajax request in the first place (this could be that immediately after the OPTIONS request, nodejs is restarted by nodemon. Then the GET request is repeated, also with 200, and the web page gets the result
The problem is that, for some reason I can't fathom, two ajax requests are made. The first one is an OPTIONS request and the second one is the GET request that I actually wanted. Any idea why?
Because you're making a cross-origin call (for instance, from http://localhost to http://localhost:someport), which is normally disallowed by the Same Origin Policy. So the browser sends a "pre-flight" OPTIONS request to see if the server wants to allow the call via Cross-Origin Resource Sharing.

Why does Fetch API Send the first PUT request as OPTIONS

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?
See the Fetch Standard, section 4.7. CORS-preflight fetch.
Note: This is effectively the user agent implementation of the check to see if the CORS protocol is understood. The so-called CORS-preflight request. If successful it populates the CORS-preflight cache to minimize the number of these fetches.
at steps 1 through 7; also 4.8. CORS-preflight cache.

Ionic Angular Js making 2 requests to server

Using Ionic framework 1,
Making request using $http.post(); but, in the console.. two requests are getting generated.
the first request doesn't contain the POST parameters and is blank , while the second request contains the POST parameters as passed to $http.post() method.
What could be the possible reason ?
May be it's checking if already cached resource is updated or not by making a blank request to the server, before making a request to load data.
But, I have not enabled any caching or anything. Everything is default.
The first request is the Request Method: OPTIONS request. It checks whether or not the actual request is safe to send. It is also called 'preflighted request'

Making post request from redirection in node.js

I am trying to redirect to another URL from node js by using response.writeHead method
response.writeHead(301, {Location : <redirecturl>})
I need to have this redirection is being executed by POST method, however it is always executed by GET. What is the way that the redirection can be made as a POST request.
A redirect by default sends a GET request. According to the HTTP spec, a 301 redirection means sending a GET request. If you want to redirect with the same method (POST), you can try doing a 307 redirect instead.
There is no difference between redirection in both GET and POST methods. Both method should work find. Better you can your expressjs framework in which it is
res.redirect('http://example.com');
Be careful, when using status code 301 which means Moved Perman­ently. Once browser gets 301 status code, it will directly try the redirected URL from the next time.
Without seeing more of your code, I believe this is what you are describing:
The client has made a request to your application using an HTTP method (get, post, etc.) You are responding to that request by sending back a 301 error and a new URL (redirecturl)
The client then decides to implement a get request for the redirecturl.
You can't change how a client responds to a 301. That is out of your control and it is normal for browsers to initiate a get for the redirecturl.
You could initiate a post request from your server to the redirecturl.
You could send back a webpage which would then submit a post request from the client.
What are you trying to achieve?

Categories

Resources