Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
HTML5 brings many features and improvements to web form creation. There are new attributes and input types that were introduced to help create better experiences for web users.
Form creation is done in HTML5 the same way as it was in HTML4:
HTML5為Web表單創(chuàng)建帶來(lái)了許多功能和改進(jìn)。 引入了新的屬性和輸入類(lèi)型,以幫助為網(wǎng)絡(luò)用戶(hù)創(chuàng)造更好的體驗(yàn)。
HTML中的表單創(chuàng)建方式與HTML4中一樣:
HTML5 has introduced a new attribute called placeholder. On <input> and <textarea> elements, this attribute provides a hint to the user of what information can be entered into the field.
HTML5引入了一個(gè)名為placeholder的新屬性。 在<input>和<textarea>元素上,此屬性向用戶(hù)提供可以在該字段中輸入哪些信息的提示。
The autofocus attribute makes the desired input focus when the form loads:
The "required" attribute is used to make the input elements required.
The form will not be submitted without filling in the required fields.
The autocomplete attribute specifies whether a form or input field should have autocomplete turned on or off.When autocomplete is on, the browser automatically complete values based on values that the user has entered before.
autocomplete屬性指定表單或輸入字段是否應(yīng)該啟用或禁用自動(dòng)完成。
當(dāng)自動(dòng)填充開(kāi)啟時(shí),瀏覽器會(huì)根據(jù)用戶(hù)之前輸入的值自動(dòng)完成值。
HTML5添加了幾種新的輸入類(lèi)型:- color- date- datetime- datetime-local- email- month- number- range- search- tel- time- url- weekNew input attributes in HTML5:- autofocus- form- formaction- formenctype- formmethod- formnovalidate- formtarget- height and width- list- min and max- multiple- pattern (regexp)- placeholder- required- step
Search Options
The <datalist> tag can be used to define a list of pre-defined options for the search field:
<datalist>標(biāo)簽可用于定義搜索字段的預(yù)定義選項(xiàng)列表:
<option> defines the options in a drop-down list for the user to select. The ID of the datalist element must match with the list attribute of the input box.
<option>在用戶(hù)選擇的下拉列表中定義選項(xiàng)。
datalist元素的ID必須與輸入框的list屬性相匹配。
Some other new input types include email, url, and tel:
These are especially useful when opening a page on a modern mobile device, which recognizes the input types and opens a corresponding keyboard matching the field's type:
當(dāng)在現(xiàn)代移動(dòng)設(shè)備上打開(kāi)頁(yè)面時(shí),這些功能特別有用,它識(shí)別輸入類(lèi)型并打開(kāi)與字段類(lèi)型匹配的相應(yīng)鍵盤(pán):
These new types make it easier to structure and validate HTML forms.
這些新類(lèi)型使得HTML表單更易于構(gòu)造和驗(yàn)證。
文地址:https://www.cnblogs.com/zhanglei93/p/6273655.html
作者:best.lei
數(shù)據(jù)綁定和表單標(biāo)簽
數(shù)據(jù)綁定是將用戶(hù)輸入綁定到領(lǐng)域模型的一種特性,在Spring MVC的controller和view數(shù)據(jù)傳遞中,基于HTTP請(qǐng)求的特性,所有HTTP請(qǐng)求參數(shù)的類(lèi)型均為字符串,如果模型領(lǐng)域需要綁定的類(lèi)型為double或int,則需要手動(dòng)進(jìn)行類(lèi)型轉(zhuǎn)換,而有了數(shù)據(jù)綁定后,就不需要手動(dòng)將HTTP請(qǐng)求中的String類(lèi)型轉(zhuǎn)換為模型需要的類(lèi)型了,數(shù)據(jù)綁定的另一個(gè)好處是,當(dāng)輸入驗(yàn)證失敗時(shí),會(huì)重新生成一個(gè)HTML表單,無(wú)需重新填寫(xiě)輸入字段。
表單標(biāo)簽庫(kù)中包含了可以用在JSP頁(yè)面中渲染HTML元素的標(biāo)簽。為了使用這些標(biāo)簽,必須在JSP頁(yè)面開(kāi)頭處聲明taglib指令。
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
表單標(biāo)簽庫(kù)中有input、password、hidden、textarea、checkbox、checkboxes、radiobutton、radiobuttons、select、option、options、errors。表單標(biāo)簽有acceptCharset、commandName、cssClass、cssStyle、htmlEscape、modelAttribute等屬性。
<form:form modelAttribute="user" method="post" action="userSave"> <fieldset> <p> <label for="name">用戶(hù)名:</label> <form:input path="userName"/> </p> </fieldset> </form:form>
如上分別介紹了數(shù)據(jù)綁定的定義和優(yōu)勢(shì),以及一些表單標(biāo)簽,為了讓大家能進(jìn)一步了解上面的內(nèi)容,范例中實(shí)現(xiàn)了用戶(hù)類(lèi)屬性和JSP中表單的綁定,同時(shí)在JSP中分別展示了input、password、textarea、checkbox、select標(biāo)簽。
我們首先看一下User類(lèi),類(lèi)中包含User的屬性,以及對(duì)屬性字段的get和set方法:
public class User { private String userName; //用戶(hù)名 private String password; //密碼 private Integer sex; //性別 private boolean marriage; //是否結(jié)婚 private ArrayList<String> hobby; //興趣愛(ài)好 private ArrayList<String> contacts;//人脈 private String carrer; //職業(yè) private String houseRegister; //戶(hù)籍 private String remark; //個(gè)人描述 public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getHouseRegister() { return houseRegister; } public void setHouseRegister(String houseRegister) { this.houseRegister = houseRegister; } public String getCarrer() { return carrer; } public void setCarrer(String carrer) { this.carrer = carrer; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public boolean isMarriage() { return marriage; } public void setMarriage(boolean marriage) { this.marriage = marriage; } public ArrayList<String> getHobby() { return hobby; } public void setHobby(ArrayList<String> hobby) { this.hobby = hobby; } public ArrayList<String> getContacts() { return contacts; } public void setContacts(ArrayList<String> contacts) { this.contacts = contacts; } }
我們的Controller類(lèi)中定義映射請(qǐng)求的方法,其中包括處理userInput請(qǐng)求的inputUser方法,以及userSave請(qǐng)求的addUser方法,其中在addUser方法中用到了重定向。其中通過(guò)@Autowired注釋在ProductController對(duì)象中主動(dòng)注入U(xiǎn)serService對(duì)象,實(shí)現(xiàn)對(duì)user對(duì)象的保存和查詢(xún)等操作;通過(guò)model的addAttribute方法將User類(lèi)對(duì)象、HashMap類(lèi)型的hobbys對(duì)象、String[]類(lèi)型的carrers對(duì)象傳遞給View(JSP),代碼如下:
同時(shí),為了避免中文亂碼的問(wèn)題,需要在web.xml文件中增加編碼過(guò)濾器,同時(shí)JSP頁(yè)面編碼設(shè)置為UTF-8,form表單的提交方式必須為post,我們先看web.xml文件的配置信息:
UserAddJSP文件格式如下,其中將Map類(lèi)型的hobbys綁定到了checkboxes上,將String[]類(lèi)型的carrer綁定到了select上,實(shí)現(xiàn)了通過(guò)option標(biāo)簽對(duì)select添加選項(xiàng),同時(shí)method方法需指定為post來(lái)避免中文亂碼的問(wèn)題。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Add a User</title> </head> <body> <div id="global"> <form:form modelAttribute="user" method="post" action="userSave"> <fieldset> <legend>Add a User</legend> <p> <label>用戶(hù)名:</label> <form:input path="userName"/> </p> <p> <label>密碼:</label> <form:password path="password"/> </p> <p> <label>婚姻:</label> <form:checkbox path="marriage" value="已婚"/> </p> <p> <label>愛(ài)好:</label> <form:checkboxes items="${hobbys}" path="hobby"/> </p> <p> <label>人脈:</label> <form:checkbox path="contacts" value="張三"/>張三 <form:checkbox path="contacts" value="李四"/>李四 <form:checkbox path="contacts" value="王五"/>王五 <form:checkbox path="contacts" value="趙六"/>趙六 </p> <p> <label>職業(yè):</label> <form:select path="carrer"> <option/>請(qǐng)選擇職業(yè) <form:options items="${carrers }"/> </form:select> </p> <p> <label>戶(hù)籍:</label> <form:select path="houseRegister"> <option>請(qǐng)選擇戶(hù)籍</option> <option value="1">北京</option> <option value="2">上海</option> <option value="3">廣州</option> <option value="4">深圳</option> <option value="5">其它</option> </form:select> </p> <p> <label>個(gè)人描述:</label> <form:textarea path="remark" rows="5"/> </p> <p id="buttons"> <input id="reset" type="reset"> <input id="submit" type="submit" value="Add User"> </p> </fieldset> </form:form> </div> </body> </html>
UserList.JSP文件實(shí)現(xiàn)對(duì)保存的user信息的遍歷展示。
因?yàn)樵摴こ淌窃谏弦黄こ讨性黾拥模虼似渌呐渲梦募c上一篇工程中相同,這里不再做過(guò)多的贅述,希望讀者見(jiàn)諒。
運(yùn)行該程序在瀏覽器中輸入http://localhost:8081/SpringMVC/userInput可以看到左圖,填寫(xiě)表單完成,點(diǎn)擊Add User按鈕,即可看到右圖的輸出信息,不知道讀者是否發(fā)現(xiàn)輸出信息中存在問(wèn)題,興趣愛(ài)好為[1],戶(hù)籍也為1,輸出的為表單中選擇的索引位置,還有好像保單中忘了指定User的性別了,導(dǎo)致輸入性別列為空。這些問(wèn)題是由于本文的疏忽造成的,有興趣的讀者可以對(duì)這篇博客做更加完善的改正,謝謝閱讀!
天內(nèi)容好長(zhǎng),翻譯起來(lái)有點(diǎn)累。
Preparations
With the basics of HTTP out of the way, let's get acquainted with the tools we'll use in this book to demonstrate how HTTP works. This section goes through a few tools you'll need to follow along. Note that you only need one of the tools listed. It is important to note that you will be able to follow this book regardless of what tool you use, so pick one that you're comfortable with and get started!
翻譯:
準(zhǔn)備工作,我們先學(xué)習(xí)下工具來(lái)演練 HTTP怎么工作的。本章節(jié)介紹下一些工具,可以選擇一個(gè)你覺(jué)得比較合適的來(lái)上手。
HTTP GUI Tools
We'll make heavy use of Graphical HTTP tools throughout this book. There are lots of options available in this category of HTTP tools, and we'll be using Paw 3. Although it's a paid App in the Mac App Store, there is a limited Trial version as well which can get you through the book.
That said, there are many other alternatives out there. Some other capable options are Insomnia and Postman; they're both free and they work on OS X, Windows, and Ubuntu.
HTTP 的 圖形用戶(hù)界面工具
我們這里用的是Paw3, 雖然他需要在蘋(píng)果商店買(mǎi),但是也有短時(shí)間免費(fèi)版本。
你也可以選擇 Insomnia 和 Postman (譯者之前裝過(guò)postman,這里就用Postman了)
HTTP Command line Tools
curl is a free command line tool that is used to issue HTTP requests.
Mac OS X/Linux :
It is shipped with OS X and most GNU/Linux distributions, and you can simply invoke it on the command line by issuing the command below.
$ curl www.google.com
If you have version 1803 or later of Windows 10, cURL should be installed by default. If you have an earlier version of windows or cannot find cURL on your version, we recommend installing a GUI version of cURL. You won't be able to execute the command line commands, but you should still be able to perform the appropriate actions in the GUI.
curl 是一個(gè)免費(fèi)的工具,用來(lái)發(fā)送HTTP 請(qǐng)求,和MAC 及絕大多數(shù) Linux 系統(tǒng)原生綁定。
你可以很簡(jiǎn)單的啟動(dòng)curl ,用下面命令行:
$ curl www.google.com
如果你有windows10的 1803 及以后版本,curl 也是默認(rèn)安裝好了,如果你有更早的版本,你可以考慮安裝一個(gè),圖形界面的curl. 你雖然沒(méi)法執(zhí)行命令,但是可以在GUI 中執(zhí)行相關(guān)操作。
Making HTTP Requests
HTTP Request with a Browser
Making an HTTP request is easy. Say you want to visit Reddit in your browser. All you need to do is launch your browser and enter the address https://www.reddit.com and
this is a snapshot of what you might see:
翻譯:
做HTTP的申請(qǐng)
通過(guò)一個(gè)瀏覽器做HTTP請(qǐng)求,做HTTP請(qǐng)求很容易,你可以在瀏覽器中訪問(wèn)reddit ,直接鍵入地址https://www.reddit.com, 然后看到快照。
The server that hosts the main Reddit website handles your request and issues a response back to your browser. Your browser is smart enough to process the response that is sent back and display the site you see in the screenshot, with all its colors, images, text and presentation.
翻譯:
承載著Reddit web主站的服務(wù)器負(fù)責(zé)處理請(qǐng)求,并返回一個(gè)response 到瀏覽器,瀏覽器足夠“聰明”可以處理返回的響應(yīng)文件,在屏幕上把文件展示出來(lái),包含的是顏色、圖片、文字和展示。
HTTP Request with an HTTP Tool
Because browsers show us the processed version of the response, we don't get to see the raw response the server sent back. How do we see the raw HTTP response data?
For that, we can use an HTTP tool and just like the browser did when we entered a URL in the address bar, we can have our HTTP tool issue a request to https://www.reddit.com.
Our HTTP tool, Paw, doesn't process the response and lets us see the raw response data, which looks something like this:
通過(guò)工具來(lái)做HTTP請(qǐng)求
因?yàn)闉g覽器顯示的是服務(wù)端回復(fù)(response)被 處理后的版本,我們沒(méi)拿到服務(wù)器發(fā)回來(lái)原始的回復(fù),我們?cè)趺床榭?服務(wù)器返回的HTTP 裸回復(fù)呢?
我們可以用HTTP工具 來(lái)實(shí)現(xiàn),我們?cè)诘刂窓谳斎險(xiǎn)RL, 我們的HTTP工具會(huì)發(fā)送一個(gè)請(qǐng)求到 https://www.reddit.com.我們的HTTP 工具,Paw,不會(huì)處理服務(wù)端的回復(fù)(response),那原始回復(fù)的response 是什么樣呢?如下圖
What a huge difference this raw response is from the display in your browser! If you've never seen raw HTTP response data before, this may be quite shocking. What you see here is, in fact, what your browser also receives, except it parses and processes that huge blob of data into a user-friendly format.
If you're learning about HTTP in order to become a web developer, you'll need to learn to read and process raw HTTP response data just by scanning it. Of course, you won't be able to convert it into a high-resolution picture in your head, but you should have a general idea of what the response is about. With enough experience, you can dig into the raw data and do some debugging and see exactly what's in the response.
這個(gè)和在瀏覽器上顯示的有很大的區(qū)別! 你的瀏覽器也會(huì)受到一樣的裸回復(fù)(response)數(shù)據(jù),瀏覽器可以對(duì)回復(fù)數(shù)據(jù)進(jìn)行解析,并以一個(gè)友好的方式向用戶(hù)呈現(xiàn)出來(lái)。
如果你在學(xué)習(xí)HTTP ,目的是做一個(gè)web 開(kāi)發(fā)者,你需要學(xué)習(xí)和讀懂如何處理這些裸數(shù)據(jù)(response).當(dāng)然,你無(wú)法把response轉(zhuǎn)換為頭腦中一個(gè)高精度的照片,但是你應(yīng)該大概了解這個(gè)回復(fù)是什么含義。只要有足夠的經(jīng)驗(yàn),你就可以閱讀這些裸數(shù)據(jù),并做一些調(diào)試工作,看看回復(fù)(response)是什么。
我在Postman 里面輸入地址后,打開(kāi)console,也可以看到下面返回的裸回復(fù)。
Using the Inspector
Every modern browser has a way to view HTTP requests and responses, and it's usually called the inspector. We're going to use the Chrome Inspector to demonstrate how to analyze your browser's HTTP communication.
Launch Chrome browser and open the Inspector by navigating to the Chrome Menu on the top right-hand corner of your browser. Select Tools > More Tools > Developer Tools. There are other ways of accessing the inspector, such as right-clicking and selecting the 'Inspector' option, or using a keyboard shortcut Ctrl+Shift+I (or Option+Command+I on a Mac).
Send a new request to Reddit by entering the address https://www.reddit.com into your browser.
With the inspector still open click on the Network tab:
翻譯:
使用檢查員(inspector)
每一個(gè)現(xiàn)代的瀏覽器都有辦法可以讀HTTP 請(qǐng)求和回復(fù)請(qǐng)求,這個(gè)方式就是檢查員。我們將使用google 瀏覽器Chrome 的檢查員(inspector)來(lái)分析瀏覽器的HTTP通信。
國(guó)內(nèi)訪問(wèn),果然時(shí)間要長(zhǎng)很多。
4. The first thing you should notice is that there are a lot of entries there. Each entry is a separate request, which means just by visiting the URL,
your browser is making multiple requests, one for every resource (image, file, etc.). Click on the first request for the main page, www.reddit.com entry:
翻譯:
首先需要知道的是,這里很多的條目,每個(gè)條目是一個(gè)單獨(dú)的請(qǐng)求。這意味著(敲黑板),即使只是訪問(wèn)url,你的瀏覽器已經(jīng)做了多個(gè)request,針對(duì)每個(gè)資源會(huì)申請(qǐng)一次(資源包括圖片、文件等等)。鼠標(biāo)點(diǎn)擊主頁(yè)條目看一下,www.reddit.com 條目:
From here, you'll be able to see the specific request headers, cookies as well as the raw response data:
翻譯:
這里可以看到針對(duì)這個(gè)主頁(yè)的請(qǐng)求,cookie 以及裸回復(fù)(response)。
The response data should look similar to what we saw earlier using our HTTP tool, except Chrome displays the data in a single line.
Another thing to note when using the inspector's Network tab is, other than the first request, there are a ton of other requests returned:
翻譯:
回復(fù)的數(shù)據(jù)應(yīng)該和之前我們用HTTP 工具 看到的內(nèi)容是相似的,區(qū)別是google 瀏覽器在單行中顯示數(shù)據(jù)。(這里在新版本google 瀏覽器已經(jīng)改過(guò)來(lái)了)
另外一個(gè)需要關(guān)注的是,使用inspector network 這個(gè)表,除了看到第一個(gè)請(qǐng)求,還有非常多的其它請(qǐng)求返回。(如下圖)
Why are these additional responses sent back, who initiated the requests? What's happening is that the resource we requested, the initial www.reddit.com entry, returned some HTML. And in that HTML body are references to other resources like images, css stylesheets, javascript files and more. Your browser, being smart and helpful, understands that in order to produce a visually appealing presentation, it has to go and grab all these referenced resources. Hence, the browser will make separate requests for each resource referenced in the initial response. When you scroll down the Network tab, you'll be able to see all the referenced resources. These other requests are to make sure the page displays properly on your screen, among other things. Overall, you see that the browser's inspector gives you a good feel for these referenced resources. A pure HTTP tool, on the other hand, returns one huge response chunk without any concern for automatically pulling in referenced resources. A curl request will demonstrate this:
翻譯:
為什么會(huì)有額外的回復(fù)被發(fā)回來(lái)? 第一個(gè)請(qǐng)求條目,www.reddit.com 返回來(lái)一些HTML 的文件。這個(gè)文件指向了其它的資源,比如圖片、CSS、JS 文件。而你的瀏覽器足夠聰明和幫助,理解了這個(gè)HTML,并去抓去了所有相關(guān)的資源。
這樣,在最開(kāi)始的回復(fù)后,瀏覽器會(huì)針對(duì)每個(gè)資源做單獨(dú)的申請(qǐng)。當(dāng)你在network 表往下查看所有的表,你能夠看到所有被引用的資源。這些其它的請(qǐng)求可以幫助你把頁(yè)面在你的屏幕顯示的更加清晰,總的來(lái)看,瀏覽器inspector 給你一個(gè)好的感覺(jué),針對(duì)所有的資源。
一個(gè)純HTTP 工具,只會(huì)返回一個(gè)巨大的response 片段,不會(huì)考慮自動(dòng)的去把關(guān)聯(lián)到的資源拉群進(jìn)來(lái)。一個(gè)curl 工具一般顯示的內(nèi)容如下:
Reddit now requires that we add in a User-Agent to our HTTP requests. Otherwise, it will deny our request, assuming that the request originates from a bot.
Make sure to append the following to any curl commands where reddit is the site you wish to send a request to.
-A 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36'
The -A option is used to specify a User-Agent for an HTTP request when using curl. Since this is another option for our command,
don't forget to add in a space between -v and -A.
For the sake of simplicity, we specify the User-Agent that is listed at the end of this page. You may use your own User-Agent as well.
$ curl -X GET "https://www.reddit.com/" -m 30 -v
What you should see is just one request and the response containing the HTML, but no additional requests being automatically issued, like you see in a browser.
翻譯:
下圖,curl 百度的效果圖:
Request Methods
Let's revisit the diagram from Step 3 above, when we looked at the responses in the Network tab. You might have noticed two columns named Method and Status.
If you don't see the Methodcolumn, it may be hidden by default. To display the Method column, right click on Status and select Method.
The Method column should now be visible next to the Status column.
翻譯:
請(qǐng)求方法
讓我們重新調(diào)到第三步(在google瀏覽器inspector那一步),我們?cè)趎etwork 里面查看responses的時(shí)候,你可以看到兩列:method 和 status.在status 狀態(tài)上右鍵,選擇展示method,如下圖。
We'll spend this section looking at what the information shown in these columns mean.
Information displayed in the Method column is known as the HTTP Request Method. You can think of this as the verb that tells the server what action to perform on a resource. The two most common HTTP request methods you'll see are GET and POST. When you think about retrieving information, think GET, which is the most used HTTP request method. In the above diagram, you'll notice almost all of the requests use GET to retrieve the resources needed to display the web page.
The Status column shows the response status for each request. We'll talk about responses in detail later in the book. The important thing to understand is that every request gets a response, even if the response is an error -- that's still a response. (That's not 100% technically true as some requests can time out, but we'll set those rare cases aside for now.)
我們將花一點(diǎn)時(shí)間介紹下這些列里包含信息的含義。
當(dāng)你想獲取信息,用GET 方法,這是最常用的HTTP 請(qǐng)求方法,在上面的請(qǐng)求中,你可以看到幾乎所有的請(qǐng)求都是GET .狀態(tài)列顯示 每個(gè)請(qǐng)求的返回狀態(tài),我們?cè)诒緯?shū)稍后會(huì)介紹狀態(tài)的含義。需要了解的是,每一個(gè)請(qǐng)求都會(huì)有一個(gè)回復(fù)(response),即使回復(fù)還是錯(cuò)誤的,這仍然是一個(gè)回復(fù)。(當(dāng)然有些超時(shí)的除外)
GET Requests
GET requests are initiated by clicking a link or via the address bar of a browser. When you type an address like https://www.reddit.com into the address bar of your browser, you're making a GETrequest. You're asking the web browser to go retrieve the resource at that address, which means we've been making GET requests throughout this book. The same goes for interacting with links on web applications. The default behavior of a link is to issue a GET request to a URL. Let's make a simple GET request to https://www.reddit.com with an HTTP tool. Make sure to select GET and enter the address:
You can view the raw HTTP response and other information sent back from the web server on the right panel.curl users can enter the following command on their terminal:
$ curl -X GET "https://www.reddit.com/" -m 30 -v
We can also send query strings using an HTTP tool. Let's look at another quick example by sending a request to search for all things Michael Jackson at https://itunes.apple.com/ with query strings. The final URL will look like this:
https://itunes.apple.com/search?term=Michael%20Jackson
before submitting a request, make sure to select GET.
Here we are simply sending an HTTP GET request to the server at https://itunes.apple.com/ with parameter term=Michael%20Jackson where %20 is a URL-encoded character for SPACE.
The curl command for this example is:
$ curl -X GET "https://itunes.apple.com/search?term=Michael%20Jackson" -m 30 -v
That's all you need to know about issuing HTTP GET requests for now. The primary concepts are:
翻譯
你需要了解的是:
POST Requests
We've seen how to retrieve or ask for information from a server with GET, but what if you need to send or submit data to the server? That's where another essential HTTP request method comes in: POST. POST is used when you want to initiate some action on the server, or send data to a server. Let's see an example with our HTTP tool:
翻譯:
如果需要提交信息到服務(wù)器,這個(gè)時(shí)候需要使用HTTP 請(qǐng)求的另外一個(gè)方法,POST。 當(dāng)你需要對(duì)服務(wù)器發(fā)起某些操作,或者發(fā)送數(shù)據(jù)到服務(wù)器,需要POST。
Here is the curl command:
$ curl -X POST "https://echo.epa.gov" -m 30 -v
The above screenshot shows a POST request to https://echo.epa.gov and the response from the server. Typically from within a browser, you use POST when submitting a form. POST requests allow us to send much larger and sensitive data to the server, such as images or videos. For example, say we need to send our username and password to the server for authentication. We could use a GET request and send it through query strings. The flaw with this approach is obvious: our credentials become exposed instantly in the URL; that isn't what we want. Using a POST request in a form fixes this problem. POST requests also help sidestep the query string size limitation that you have with GET requests. With POST requests, we can send significantly larger forms of information to the server.
Let's see another example of making a POST request by filling out a web form. Our sample form looks like this in the browser:
翻譯:
curl 命令行如下:
$ curl -X POST "https://echo.epa.gov" -m 30 -v
After filling out the form, you'll be redirected to a page that looks like this:
Now let's switch over to our HTTP tool and simulate what we just did in the browser. Instead of filling out a form in the browser, we will send a POST request to http://al-blackjack.herokuapp.com/new_player. This is the URL that the first form (the one where we input a name) submits to:
翻譯:
我們換到HTTP 工具上來(lái)模擬我們?cè)跒g覽器上的操作,代替我們?cè)跒g覽器上填入信息,我們會(huì)發(fā)送Post 請(qǐng)求到 http://al-blackjack.herokuapp.com/new_player
我們第一個(gè)表格(我們輸入名字的)提交到這個(gè)url:
Note: You'll want to ensure that your Content-Type header is set to application/x-www-form-urlencoded. If it isn't, then your POST request won't be interpreted by the application correctly.
If you're using Paw 3, select the Form URL-Encoded tab instead of the Text tab.
If you're using Insomnia, make sure you click "Form URL Encoded" in the Body dropdown menu. And if you're using Postman, make sure the radio button for x-www-form-urlencodedis selected under the Body tab.
翻譯:
你會(huì)希望保證你的 content-type header 為 application/x-www-form-urlencoded.如果不這樣設(shè)置,服務(wù)器將無(wú)法識(shí)別請(qǐng)求。
如果選擇postman,記得選擇了 x-www-form-urlencode 按鈕,在body表中。
譯者在postman中的操作截圖:
303 see other的含義:(from 譯者)
Or you can use curl:
$ curl -X POST "http://al-blackjack.herokuapp.com/new_player" -d "player_name=Albert" -m 30 -v
Notice that in the screenshot and curl command we're supplying the additional parameter of player_name=albert. It has the same effect as inputting the name into the first "What's your name?" form and submitting it.
We can verify the contents using the inspector (right click and select Inspect). You'll see that the player_name parameter we're sending as part of the POST request is embedded in the form via the nameattribute of the input element:
翻譯:
或者你可以用curl 命令實(shí)現(xiàn) :
$ curl -X POST "http://al-blackjack.herokuapp.com/new_player" -d "player_name=Albert" -m 30 -v
這里 -d "player_name=Albert" 和在瀏覽器里面填入 “what's your name?”表格效果是一樣的,可以在瀏覽器里面看到, player_name 參數(shù)是保存在表格里的。
But the mystery is, how is the data we're sending being submitted to the server since it's no
t being sent through the URL? The answer to that is the HTTP body.
The body contains the data that is being transmitted in an HTTP message and is optional. In other words, an HTTP message can be sent with an empty body.
When used, the body can contain HTML, images, audio and so on. You can think of the body as the letter enclosed in an envelope, to be posted.
The POST request generated by the HTTP tool or curl is the same as you filling out the form in the browser, submitting that form, and then being redirected to the next page.
Look carefully at the raw response in the HTTP tool screenshot. The key piece of information that redirects us to the next page is specified in the field Location:
http://al-blackjack.herokuapp.com/bet. Location and its associated data is part of what is known as an HTTP response header (yes, requests have headers too, but in this case,
it's a response header). Don't worry too much about this yet as we'll discuss headers in a later section. Your browser sees the response header and automatically issues a brand
new request to the URL specified in the Location header, thereby initiating a new, unrelated request. The "Make a bet" form you see is the response from that second request.
翻譯:
不過(guò)疑問(wèn)就在,這個(gè)數(shù)據(jù)是怎么提交到服務(wù)器上的,因?yàn)樗麤](méi)有通過(guò)URL 提交。
答案是:HTTP body (HTTP 身體)(敲黑板),HTTP body包含了傳遞給HTTP的數(shù)據(jù),這個(gè)是可選的。
http://al-blackjack.herokuapp.com/bet. ( request 請(qǐng)求有Header ,這里是response 的header信息)你的瀏覽器看到了response header, 自動(dòng)發(fā)起了一個(gè)新的請(qǐng)求到url (location 字段里指定的),這樣就看到了第二個(gè)response 表格。
Note: If you're using some other HTTP tool, like Insomnia or Postman, you may have to uncheck "automatically follow redirects" in order to see the Location response header.
If you're fuzzy on the previous paragraph, read it again. It's critical to understand that when using a browser, the browser hides a lot of the underlying HTTP request/response cycle from you. Your browser issued the initial POST request, got a response with a Location header, then issued another request without any action from you, then displayed the response from that second request. Once again, if you were using a pure HTTP tool, you'd see the Location response header from the first POST request, but the tool would not automatically issue a second request for you. (Some HTTP tools have this ability, if you check the "automatically follow redirects" option.)
翻譯:
這里就是對(duì)上文的重復(fù)解釋?zhuān)粡?fù)述了。
HTTP Headers
HTTP headers allow the client and the server to send additional information during the request/response HTTP cycle. Headers are colon-separated name-value pairs that are sent in plain text. By using the Inspector, we can see these Headers. Below, you can see both the request as well as the response headers:
The above shows the various headers being transmitted during a request/response cycle. Further, we can see that the request and response contain a different set of headers under Request Headers:
The above shows the various headers being transmitted during a request/response cycle.
Further, we can see that the request and response contain a different set of headers under Request Headers:
Request Headers
Request headers give more information about the client and the resource to be fetched. Some useful request headers are:
Don't bother memorizing any of the request headers, but just know that it's part of the request being sent to the server. We'll talk about response headers in the next chapter.
翻譯:
這里講了request header 的一些信息,request header 和 response header 信息是不一樣的。
上圖里request header 里面包含有:主機(jī)名、接受的語(yǔ)言、用戶(hù)端agent 信息、連接信息。
Summary
This was a brief introduction on making HTTP requests. After going through this section, you should be comfortable with:
In the next chapter, we'll continue learning about HTTP by looking at HTTP responses.
翻譯:
本章節(jié)介紹了下HTTP requests 信息,
經(jīng)過(guò)本章節(jié),你可以:
使用inspector 來(lái) 查看HTTP requests 請(qǐng)求
使用HTTP 工具來(lái)發(fā)起GET /POST 請(qǐng)求
最需要了解的組件包括:
HTTP 方法
路徑
headers 頭部
消息體(針對(duì)post 請(qǐng)求)
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。