Chapter 3 HTTP

網路連結下,電腦與電腦如何講悄悄話來傳遞訊息?

3.1 學習目標

  • 了解網路資料傳輸協定HTTP。

  • 在R裡如何進行HTTP資料傳輸。

  • API應用實作:LINE,Facebook等。

Read:

Reference:

3.2 網路通訊協定 HTTPS (HTTP)

an extension of the Hypertext Transfer Protocol (HTTP). It is used for secure communication over a computer network, and is widely used on the Internet.

  • HTTP:

    an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web, where hypertext documents include hyperlinks to other resources that the user can easily access, for example by a mouse click or by tapping the screen. HTTP was developed to facilitate hypertext and the World Wide Web.

3.2.1 Request message

A typical request message that follows HTTP should have:

資源伺服器網址也叫做root endpoint

  • request header fields (e.g., Accept-Language: en).

  • an empty line

  • an optional message body

3.2.2 Request methods

Current HTTP allows GET, PUT, POST, DELETE methods, etc. Among them,

  • GET: 取得資源(資料)

  • POST: 新增資源

3.2.3 Response message

  • a status line which includes the status code and reason message (e.g., HTTP/1.1 200 OK, which indicates that the client’s request succeeded.)

  • response header fields (e.g., Content-Type: text/html)

  • an empty line

  • an optional message body

我們平日所使用的瀏覽器是在進行GET請求,得到訊息後瀏覽器再以我們可理解文字圖像呈現在眼前。

Response content常見有以下三種格式:

  • 文字:網址回傳一個文字檔時。

  • JSON:網址回傳一串文字資訊,如REST API。

  • Binary:網址回傳一個「非文字檔」。

3.3 curl

curl (client URL): a command-line tool for getting or sending data including files using URL syntax.

  • 讓個人電腦也可以進行各網路通訊協定的資訊交換。

  • 使用terminal視窗

3.4 httr

R裡用來操作curl的套件。

3.5 httr: curl online translator

https://curl.trillworks.com/

  • 可翻譯多語言

3.6 httr: curlconverter

R套件,也是RStudio Addins,只要電腦裡有複製的cur語句(不一定要在RStudio裡), 它會幫你翻成httr語法。

3.8 HERE geo API

3.10 Facebook API

3.13 Hypothes.is project