Skip to content

HTTP

学习 HTTP — protocol 那个 transfers 每个 web page, and 状态 codes 那个描述响应.

你将学到

  • 什么 HTTP 是 and 如何请求 and 响应工作
  • meaning 的常见 HTTP 方法 (GET, POST, PUT, DELETE)
  • 如何状态 codes categorize 响应
  • difference 之间 HTTP and HTTPS

概念讲解

HTTP

HTTP (HyperText Transfer Protocol) is the set of rules that governs how clients and servers communicate on the web. Every time your browser loads a page, it speaks HTTP with the server. Understanding HTTP is understanding the web's conversation.

Requests and Responses

An HTTP exchange has two parts:

Request (client → server):

GET /users/42 HTTP/1.1
Host: api.example.com
Accept: application/json

Response (server → client):

HTTP/1.1 200 OK
Content-Type: application/json

{"id": 42, "name": "Ada"}

The request has a method, a path, and headers. The response has a status code, headers, and a body.

HTTP Methods

| Method | Purpose | |--------|---------| | GET | Read data (fetch a page or resource) | | POST | Submit data (create a new resource) | | PUT | Replace a resource entirely | | PATCH | Partially update a resource | | DELETE | Remove a resource |

GET and POST are the most common. GET should never change data; POST is used for forms and creating things.

Status Codes

The first digit tells the category:

  • 2xx — Success (200 OK, 201 Created).
  • 3xx — Redirect (301 Moved, 304 Not Modified).
  • 4xx — Client error (404 Not Found, 401 Unauthorized).
  • 5xx — Server error (500 Internal Server Error, 503 Service Unavailable).

When something breaks, the status code tells you whose fault it is: 4xx means you sent a bad request; 5xx means the server failed.

HTTPS

HTTPS is HTTP encrypted with TLS. The "S" stands for Secure. Without it, your traffic travels in plaintext — anyone on the network can read passwords and cookies. Modern browsers require HTTPS for most features, and you should never send sensitive data over plain HTTP.

示例代码

              // Fetching data from an API using HTTP GET
async function getUser(id) {
  const response = await fetch(`https://api.example.com/users/${id}`);

  if (!response.ok) {
    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
  }

  const user = await response.json();
  return user;
}

// Sending data with HTTP POST
async function createUser(data) {
  const response = await fetch("https://api.example.com/users", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(data),
  });
  return response.json();
}
            

获取() 是浏览器's 构建-in HTTP 客户端. 首先 call 做 GET and parses JSON. 其次发送 POST 使用 JSON 请求体. 检查响应.好的 handles 非-2xx 状态 codes.

动手试一试

  • JWT Decoder

    JWTs travel in HTTP 授权请求头. Decode one 到 inspect claims 服务器发送.

  • Hash Generator

    ETags and integrity 检查 use hashes 发送 over HTTP. Generate one 到 see 如何内容 addressing 工作.

常见错误

错误写法

Ignoring 非-2xx HTTP 响应 in 获取().

正确做法

获取做 不 throw on 404 or 500 — 仅 on network 失败. 检查响应.好的 or 响应.状态 and handle 错误 explicitly.

错误写法

Using GET 到发送 sensitive 数据 in URL.

正确做法

URLs 是 logged 通过服务器 and proxies and 保存 in 浏览器 history. Use POST 使用请求请求体 for sensitive 数据, and 总是 use HTTPS.

相关指南

练习 HTTP

12 个练习

练习 HTTP

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误