官术网_书友最值得收藏!

Request body

Query parameters are typically only used on HTTP GET requests. For requests where you are sending data to the server, such as POST and PUT requests, you would send a request body that holds all of your extra information. Request bodies are placed after the HTTP headers in an HTTP request, with a one-line space between them. The following is a hypothetical POST request for logging into an imaginary website:

POST /login HTTP/1.1
Host: myprotectedsite.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 38

username=myuser&password=supersecretpw

In this request, we are sending our username and password to myprotectedsite.com/login. The headers for this request must describe the request body so the server is able to process it. In this case, we declare that the request body be in the x-www-form-urlencoded format, which is the same format used for the query parameters in the Query parameters section. We could use alternative formats, such as JSON or XML or even plain text, but only if it is supported by the server. The x-www-form-urlencoded format is the most widely supported and is generally a safe bet. The second parameter we define in the header is the length of the request body in bytes. This allows the server to efficiently prepare for processing the data, or rejecting the request completely if it is too large.

The Go standard library has good support for building HTTP requests quite simply—if you are familiar with the structure, at least. We will revisit how this is done later in this chapter.

主站蜘蛛池模板: 靖西县| 湾仔区| 哈密市| 西充县| 清镇市| 万源市| 旬阳县| 美姑县| 普格县| 龙南县| 长白| 丰原市| 盐源县| 于都县| 车致| 子洲县| 开阳县| 武宁县| 苏尼特右旗| 本溪市| 荆州市| 忻州市| 五峰| 枞阳县| 平利县| 思南县| 财经| 石门县| 玛纳斯县| 慈利县| 拉萨市| 湖北省| 临夏市| 公主岭市| 青阳县| 茌平县| 云南省| 靖州| 北宁市| 讷河市| 两当县|