Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http requests withCredentials what is this and why using it?

People also ask

What is the purpose of withCredentials?

withCredentials property is a boolean value that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates. Setting withCredentials has no effect on same-site requests.

What does withCredentials do in Axios?

You can use withCredentials property. XMLHttpRequest from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request.

What does Access-Control allow credentials do?

The Access-Control-Allow-Credentials response header tells browsers whether to expose the response to the frontend JavaScript code when the request's credentials mode ( Request. credentials ) is include . When a request's credentials mode ( Request.

What does credentials same origin mean?

same-origin. Send user credentials (cookies, basic http auth, etc..) if the URL is on the same origin as the calling script. This is the default value. include. Always send user credentials (cookies, basic http auth, etc..), even for cross-origin calls.


Short answer:

withCredentials() makes your browser include cookies and authentication headers in your XHR request. If your service depends on any cookie (including session cookies), it will only work with this option set.

Longer explanation:

When you issue an Ajax request to a different origin server, the browser may send an OPTIONS pre-flight request to the server to discover the CORS policy of the endpoint (for non-GET requests).

Since the request may have been triggered by a malicious script, to avoid automatically leaking authentication information to the remote server, the browser applies the following rules :

For GET requests, include cookie and authentication information in the server request :

  • if XHR client is invoked with the withCredentials option is set to true
  • and if the server reply does not include the CORS header Access-Control-Allow-Credentials: true, discard response before returning the object to Javascript

For non GET requests, include cookie and authentication information only:

  • if withCredentials is set to true on the XHR object
  • and the server has included the CORS header Access-Control-Allow-Credentials: true in the pre-flight OPTIONS

Short answer from Axios documentation

withCredentials indicates whether or not cross-site Access-Control requests should be made using credentials

Credentials are cookies, authorization headers or TLS client certificates Reference

Default value of withCredentials is false