Archive for the ‘HTTP’ Category

API and HTTP Requests

The HTTP stands for HyperText Transfer Protocol. HyperText is text with links in it and a transfer protocol is a fancy way of saying “rules for getting something from one place to another. We can use HTTP to grab information from just about any web page on the Internet. The Internet is full of clients who ask for various resources (web pages, files, and so on), and servers, who store that information (or know where to get it).

When you make an HTTP request, it zips through the Internet until it finds the server that knows how to fulfill that request. Then the server sends a response back to you. In fact, this client/server relationship is a prerequisite of a set of principles called REST (Representational State Transfer).
An API, or application programming interface, is kind of like a coding contract: it specifies the ways a program can interact with an application.

How to make a request ?

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.facebook.com/", false); // more details on open method.
xhr.send();
console.log(xhr.status); //returns 200 - HTTP STATUS code for success.
console.log(xhr.statusText); // returns OK

The number of HTTP methods are:

  • GET: retrieves information from the specified source.
  • POST: sends new information to the specified source.
  • PUT: updates existing information of the specified source.
  • DELETE: removes existing information from the specified source.

An HTTP request is made up of three parts:

The request line, which tells the server what kind of request is being sent (GET, POST, etc.) and what resource it’s looking for;
The header, which sends the server additional information (such as which client is making the request)
The body, which can be empty (as in a GET request) or contain data (if you’re POSTing or PUTing information, that information is contained here).

Endpoints are API-defined locations where particular data are stored. If you’re using the API for a video hosting service, there might be endpoints for the most popular videos, the most recent videos, etc.

Authentication & API Keys

Many APIs require an API key.an API key identifies you to the API, which helps the API provider keep track of how their service is used and prevent unauthorized or malicious activity. Some APIs require authentication using a protocol called OAuth. API keys are often long alphanumeric strings.

eg: var apiKey = “FtHwuH8w1RDjQpOr0y0gF3AWm8sRsRzncK3hHh9”;

HTTP Status Codes

A successful request to the server results in a response, which is the message the server sends back to you, the client. The response from the server will contain a three-digit status code. These codes can start with a 1, 2, 3, 4, or 5, and each set of codes means something different.

1xx: You won’t see these a lot. The server is saying, “Got it! I’m working on your request.”

2xx: These mean “okay!” The server sends these when it’s successfully responding to your request.

3xx: These mean “I can do what you want, but I have to do something else first.” You might see this if a website has changed addresses and you’re using the old one; the server might have to reroute the request before it can get you the resource you asked for.

4xx: These mean you probably made a mistake. The most famous is “404,” meaning “file not found”: you asked for a resource or web page that doesn’t exist.

5xx: These mean the server goofed up and can’t successfully respond to your request.

More  information on HTTP Status codes – here.

Structure of a Response

  • A response line, which includes the three-digit HTTP status code.
  • A header, which includes further information about the server and its response.
  • The body, which contains the text of the response.

Response can be obtained either as XML or JSON, we need to parse the corresponding response for the actual data retrieval.

Advertisements

Tag Cloud