RapidAPI Profile picture
Feb 8 โ€ข 20 tweets โ€ข 3 min read
What exactly is CORS.

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
CORS is an HTTP-based mechanism that lets you request data from one URL to a different URL.
CORS stands for Cross-Origin Resource Sharing.

Half of the definition is clear by the abbreviation. CORS allows a server to share resources with browsers having different origins.
Let's try to understand cross-origin requests via an example:

Suppose you're making an HTTP request from "a .com" to "b .com". That's a cross-origin request.
Often, you get an error while you're trying to request from a different URL.

This happens because browsers implement a same-origin policy for security reasons in which clients can make requests to the same origin without any errors. same-origin policy
Fun Fact: ๐Ÿ’ก

Popular XMLHttpRequest and the Fetch API that we all have been using for a while follow the same-origin policy.

Moving forward ๐Ÿ‘‡๐Ÿป
๐Ÿ“Œ How CORS Works

The server adds the `Access-Control-Allow-Origin` header in the response, which must be the same as the `Origin` header of the request. If this is not the case, the browser will prevent the data from being shared with the client.
A few HTTP request methods cause side effects on the server, and these types of request methods must be pre-flighted.

Let's see what exactly Preflighted requests are ๐Ÿ‘‡๐Ÿป
๐Ÿ“Œ Preflighted requests

The browser first sends the OPTIONS HTTP request to the server to ensure the actual request is safe to send.

In response, the server sends the `Access-Control-Allow-Methods` header with the allowed HTTP request methods values.
For better understanding, take a look at this infographic ๐Ÿ‘‡๐Ÿป preflight request
CORS can be tackled quickly with the understanding of a few HTTP headers. ๐Ÿ‘‡๐Ÿป
๐Ÿ“Œ HTTP Request Headers

The client can use a few HTTP request methods with their API calls in order to make maximum use of the Cross-Origin resource sharing feature.
1๏ธโƒฃ Origin

The Origin header indicates the origin of the request. Browsers add the Origin request header to all cross-origin requests.
2๏ธโƒฃ Access-Control-Request-Method

Access-Control-Request-Method header is used with the preflight request to let the server know which method will be used in the main request.

For example,

Access-Control-Request-Method: POST
๐Ÿ“Œ HTTP Response Headers

The server sends Access-Control-* HTTP headers for cross-origin requests. ๐Ÿ‘‡๐Ÿป
1๏ธโƒฃ Access-Control-Allow-Origin

Access-Control-Allow-Origin tells the browser which origin value is allowed to access the resources.

For example,

Access-Control-Allow-Origin: <origin> | *

The wildcard (*) indicates that all the origins can access the resources.
2๏ธโƒฃ Access-Control-Max-Age

The Access-Control-Max-Age header indicates the amount of time in which the result of the preflight request can be cached.

After the specified time, the browser needs to send a new preflight request.
3๏ธโƒฃ Access-Control-Allow-Credentials

Access-Control-Allow-Credentials is used with the response of a preflight request which indicates whether the actual request can be made using credentials.
4๏ธโƒฃ Access-Control-Allow-Methods

The Access-Control-Allow-Methods header indicates which methods are allowed to access the cross-origin resource. It is sent in response to a preflight request.

For example,

Access-Control-Allow-Methods: POST
With that being said, that's pretty much it for this thread.

Give it a retweet if you think this might be helpful for someone. Follow us @Rapid_API for more excellent content. ๐Ÿ˜‰

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with RapidAPI

RapidAPI Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Rapid_API

Feb 7
These ten threads will help you improve your API development skills.

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
Read 11 tweets
Feb 3
Let's break down the process of building a GraphQL server from scratch.

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
Let's start with the theoretical discussion.

Quickly take a look at this thread to get started with GraphQL. ๐Ÿ‘‡๐Ÿป

We are assuming you have Node.js installed on your machine. If you haven't installed it, click on the following link and install it simply.

nodejs.org/en/
Read 20 tweets
Feb 3
What exactly is the HTTP-based system.

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
HTTP is a communication protocol that establishes the connection between web browsers and web servers.
HTTP is entirely based on the client-server model, where the client initiates the request then waits until it receives a response from the server.
Read 19 tweets
Feb 2
What exactly is GraphQL.

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
GraphQL is a query language that lets your client request the server to send only the required data.
GraphQL was created by developers at Facebook in 2012.

GraphQL was developed to support the complicated data structures required to show the Facebook News Feed on the mobile application.
Read 18 tweets
Feb 1
What exactly is an API?

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
API is the acronym for Application Programming Interface.

In simple terms, API is an interface that lets your application talk to another application.
API is a set of definitions and protocols that allow technology products and services to communicate.

This may not sound very clear. Let's break it down.
Read 15 tweets
Jan 31
How to make your API secure.

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
Building an API is one thing, and securing it is another.

If your API is vulnerable, the user's sensitive information will be at risk.

Hence, it's crucial to deliver a secure API.
If your API is not secure, the attackers can quickly access your network by exploiting vulnerabilities in your APIs.

Before deep-diving into making your API secure, let's first discuss what are some common attacks. ๐Ÿ‘‡๐Ÿป
Read 14 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(