Http basics

  1. Http basics

    The client sends a request (request message) to the server, and the request content includes request line, request header, and request body.

    The server responds to the data returned by the client, and the http response (response message) includes status line, response header, and entity content.


    The specific content of the request can be viewed in inspect-network. The first item under All-Name (the URL item) is sent to the request. There are General, Response Headers and Request Headers in the Headers after clicking, These are the information organized by the browser.


    Request message:

    1. Request line

      method url

      GET/produc_detail?id=2

      POST/login

    2. Multiple request headers

      Host: www.google.com

      Cookie[^1]: SEARCH_SAMESITE=CgQIl5QB; HSID=Acx6gym1ffa3ppTqI; SSID=ABqoRPct1dz_XB-1y; …

      Content-Type: apllication/x-www-form-urlencoded or application/json

      Content-type is the request body content type.

    3. Request body

      The request body does not necessarily have. A GET request has no request body, a POST request [^2] has no request body without parameters, and a POST request has a request body with parameters.

      username=tom&pwd=123 This is Content-Type: apllication/x-www-form-urlencoded format.

      {“username”: “tom”, “pwd”: “123”} This is Content-Type: application/json format.


    Response message:

    1. Response status line

      status Status code, such as 200, 201, 401, 404, 500. (one-to-one correspondence with the status text example)

      statusText Status text such as OK, Created, Unauthorized, Not Found, Internal Server Error

    2. Multiple response headers

      Content-Type: text/html; (text in html format, text/json in JSON format) charest=utf-8 (encoding)

      Set-Cookie: BD_CK_SAM=1; path=/ The server returns the cookie data, corresponding to the cookie in the request header.

    3. Response body

      html text/json text/js/css/image..


  2. Different types of requests and their functions

    The same server address sends different types of requests, and the server does different things.

    1. GET: read data from the server

    2. POST: add new data to the server

    3. PATCH: Add new data to the server (partial update)

    4. PUT: Update the existing data on the server side (overall update) (in practice, it is often mixed with POST)

    5. DELETE: Delete server-side data


  3. Ajax request

    There are two ways to send Ajax requests: XHR objects, and the catch() function.


  4. Classification of API

    The API here refers to the interface for front-end and back-end interaction.

    1. REST API: restful (another name)

      1. Send a request for CRUD (CRUD: the English abbreviation of CRUD, create/read/update/delete) which operation is determined by the request method

        is actually a different type of request in section Axios.1.2. If the server supports different request types corresponding to different operations, it is restful; otherwise, it is restless.

      2. The same request path can perform multiple operations

      3. The request method will use GET/POST/PUT/DELETE

    2. Non-REST API: restless (alias)

      1. The request method does not determine the requested CRUD operation

      2. A request path corresponds to only one operation

      3. Generally only GET/POST (GET for query, POST for addition, deletion and modification)



Appendix

[^1]: If it is unsuccessful, it is a problem with the installation of json-server

There are 3 ways to solve the installation problem. The first is to add npx before the code, the second is to open a cmd in the current directory and type the code, and the third is to modify permissions through PowerShell in administrator mode [About execution policy](https://docs.microsoft. com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2).

Admin Mode PowerShell:

  1. Press Win + R.

  2. Type powershell and press Ctrl+Shift+Enter.

  3. Click OK to start PowerShell with administrator privileges.


PowerShell By default, *.ps1 script files are not allowed to be executed. Running the ps1 file gives the following error:

File C:\Temp\Test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.

The following command can resolve the above error

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned


[^2]: db.json code

1
2
3
4
5
6
7
8
9
{
"posts": [ //posts, not POST requests
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}

Share