How do I send an HTTP GET request? [JavaScript/AJAX Code]

The GET request method is used to fetch data from the server. We cannot send data in the body of an HTTP GET message, but we can still send some data to the server in URL parameters. In this HTTP GET request example, we are downloading the content of the ReqBin echo URL. The Accept: */* request header tells the server that the client accepts all media types. The Content-Type: text/html response header informs the client that the server returned HTML for this HTTP GET request. The JavaScript/AJAX code was automatically generated for the GET Request example.
How do I send an HTTP GET request? [JavaScript/AJAX Code] Send
GET /echo HTTP/1.1
Host: reqbin.com
Accept: */*

Updated: Viewed: 40351 times
JavaScript/AJAX code for GET Request example

JavaScript/AJAX code for GET Request Example

This JavaScript/AJAX code snippet was generated automatically for the GET Request example.
<< Back to the GET Request example

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is the core of the World Wide Web and powers websites and mobile applications. The HTTP is designed to transfer information between networked devices and provides a framework for client/server communication. HTTP works as a request/response protocol between client and server. For example, a browser (client) sends an HTTP GET request to a web server (server); the server then returns the response to the browser. The HTTP request contains the HTTP method (mostly GET), target URL, HTTP request headers, and additional URL parameters. The HTTP response includes information on the request's status (mostly 200), the requested content (HTML, JSON, image, etc.), and optional HTTP headers on how to interpret this content. All modern programming languages natively support HTTP, including JavaScript/AJAX.

What is HTTP GET request?

HTTP GET request method is used to retrieve data from a specified URL. The GET is the most popular HTTP request method. GET requests should only receive data and should not affect the state of the server.

HTTP GET Request Format

The GET request consists of the request-line and HTTP headers section. The GET request-line begins with an HTTP method token, followed by the request URI and the protocol version, ending with CRLF. Space characters separate the elements. Below is an example of a GET request to the ReqBin echo server and the auto-generated JavaScript/AJAX code.

HTTP GET Request Example
GET /echo HTTP/1.1
Host: reqbin.com
Accept: */*

Some notes on HTTP GET Requests

  • GET request method is used to get a resource from the server
  • GET requests cannot have a message body, but you still can send data to the server using the URL parameters
  • GET requests should only receive data. If you want to change data on the server, use POST, PUT, PATCH, or DELETE methods
  • GET method is defined as idempotent, which means that multiple identical GET requests should have the same effect as a single request

HTTP GET Request Examples

Example of getting HTML page from ReqBin echo URL with JavaScript/AJAX.

GET HTML Request Example
GET /echo HTTP/1.1
Host: reqbin.com
Accept: text/html

Example of getting JSON data from ReqBin echo URL with JavaScript/AJAX.

GET JSON Request Example
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

Example of getting XML data from ReqBin echo URL with JavaScript/AJAX.

GET XML Request Example
GET /echo/get/xml HTTP/1.1
Host: reqbin.com
Accept: application/xml

See also

Generate code snippets for JavaScript/AJAX and other programming languages

Convert your GET Request request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the JavaScript/AJAX code generator.