<commit_before>## HTTP requests in browsers

- The most common way to make HTTP request in the browser is by using the `window.XMLHttpRequest` API.
- XMLHttpRequest was originally designed by Microsoft and then adopted by all browser vendors. Despite its name, XMLHttpRequest can be used to fetch any type of data, not just XML.

## Simple example

```js
const request = new window.XMLHttpRequest()

request.addEventListener('load', e => {
  console.log('response body', e.target.status)
  console.log('response status', e.target.responseText)
})
request.addEventListener('error', e => {
  console.log('error', e)
})

request.open('GET', './url')
request.send()
```

- Once the request is successfully completed (`load` event is fired), the HTTP status code and request body are avaliable to us.
    - `event.target.status` HTTP status code as `int`.
    - `event.target.responseText` HTTP body as `string`.
    - `event.target.getAllResponseHeaders()` response headers as newline separated string.

## Customizing the request

- There are several ways to customize the request.

### The HTTP request method

- HTTP method is specified as the first argument of the `open` method.
- HTTP method name is case-insensitive.
- All HTTP methods are supported, so you can use the less common ones like `DELETE` or `PUT`.

```js
request.open('GET', './url')
```

### Setting HTTP headers

- Call the `setRequestHeader` method after `open` and before `send`.
- If no `Accept` header has been set, an `Accept` header with the `*/*` value is automatically added.
- For security reasons, some headers can not be set from JavaScript and controlled only by the browser. These headers are from the [forbidden header names](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name) and [forbidden response header](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_response_header_name) names.

```js
const request = new window.XMLHttpRequest()

request.open('GET', './url')
request.setRequestHeader('Content-Type', 'application/json')
request.setRequestHeader('Accept', 'application/json')
request.send()
```

Most common headers you might use:

```js<commit_msg>[article] ajax: grammar<commit_after>## HTTP requests in browsers

- The most common way to make HTTP request in the browser is by using the `window.XMLHttpRequest` API.
- XMLHttpRequest was originally designed by Microsoft and then adopted by all browser vendors. Despite its name, XMLHttpRequest can be used to fetch any type of data, not just XML.

## Simple example

```js
const request = new window.XMLHttpRequest()

request.addEventListener('load', e => {
  console.log('response status code', e.target.status)
  console.log('response body', e.target.responseText)
})
request.addEventListener('error', e => {
  console.log('error', e)
})

request.open('GET', './url')
request.send()
```

- Once the request is successfully completed (`load` event is fired), the HTTP status code and request body are avaliable to us.
    - `event.target.status` HTTP status code as `int`.
    - `event.target.responseText` HTTP body as `string`.
    - `event.target.getAllResponseHeaders()` response headers as newline separated `string`.

## Customizing the request

- There are several ways to customize the request.

### The HTTP request method

- HTTP method is specified as the first argument of the `open` method.
- HTTP method name is case-insensitive.
- All HTTP methods are supported, so you can use the less common ones like `DELETE` or `PUT`.

```js
request.open('GET', './url')
```

### Setting HTTP headers

- Call the `setRequestHeader` method after `open` and before `send`.
- If no `Accept` header has been set, an `Accept` header with the `*/*` value is automatically added.
- For security reasons, some headers can not be set from JavaScript and are controlled only by the browser. These headers are from the [forbidden header names](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name) and [forbidden response header](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_response_header_name) names.

```js
const request = new window.XMLHttpRequest()

request.open('GET', './url')
request.setRequestHeader('Content-Type', 'application/json')
request.setRequestHeader('Accept', 'application/json')
request.send()
```

Most common headers you might use:

```js
{'fraction_non_alphanumeric': 0.1161512027491409, 'fraction_numerical': 0.0, 'mean_word_length': 4.619047619047619, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3851457', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1185, 'n_words': 485}