什么是Webhook【轉】


一、概述

Webhook是一個API概念,並且變得越來越流行。我們能用事件描述的事物越多,webhook的作用范圍也就越大。Webhook作為一個輕量的事件處理應用,正變得越來越有用。

准確的說webhoo是一種web回調或者http的push API,是向APP或者其他應用提供實時信息的一種方式。Webhook在數據產生時立即發送數據,也就是你能實時收到數據。這一種不同於典型的API,需要用了實時性需要足夠快的輪詢。這無論是對生產還是對消費者都是高效的,唯一的缺點是初始建立困難。

Webhook有時也被稱為反向API,因為他提供了API規則,你需要設計要使用的API。Webhook將向你的應用發起http請求,典型的是post請求,應用程序由請求驅動。

二、使用webhook

消費一個webhook是為webhook准備一個URL,用於webhook發送請求。這些通常由后台頁面和或者API完成。這就意味你的應用要設置一個通過公網可以訪問的URL。

多數webhook以兩種數據格式發布數據:JSON或者XML,這需要解釋。另一種數據格式是application/x-www-form-urlencoded or multipart/form-data。這兩種方式都很容易解析,並且多數的Web應用架構都可以做這部分工作。

三、Webhook調試

調試webhook有時很復雜,因為webhook原則來說是異步的。你首先要解發他,然后等待,接着檢查是否有響應。這是枯燥並且相當低效。幸運的是還有其他方法:

1、明白webhook能提供什么,使用如RequestBin之類的工具收集webhook的請求;

2、用cURL或者Postman來模擬請求;

3、用ngrok這樣的工具測試你的代碼;

4、用Runscope工具來查看整個流程。

四、webhook安全

因為webhook發送數據到應用上公開的URL,這就給其他人找到這個URL並且發送錯誤數據的機會。你可采用技術手段,防止這樣的事情發生。最簡單的方法是采用https(TLS connection)。除了使用https外,還可以采用以下的方法進一步提高安全性:

1、首先增加Token,這個大多數webhook都支持;

2、增加認證;

3、數據簽名。

五、重要的問題

當作為webhook的消費者時有兩件事需要銘記於心:

1、webhook通過請求發送數據到你的應用后,就不再關注這些數據。也就是說如果你的應用存在問題,數據會丟失。許多webhook會處理回應,如果程序出現錯誤會重傳數據。如果你的應用處理這個請求並且依然返回一個錯誤,你的應用就會收到重復數據。

2、webhook會發出大量的請求,這樣會造成你的應用阻塞。確保你的應用能處理這些請求。

 

Webhook vs API: What’s the Difference Between Them?

As you become familiar with SendGrid’s services, or even those of many Internet companies, you’ll see the term “API” used a lot. Increasingly, you may also see someone mention a “webhook.”

At SendGrid, we’ve consciously made a distinction between the two in our documentation and any time we write or speak about our seven services for simplifying email. Here’s how we break down the differences between webhooks and APIs.

What is an API?

API stands for Application Programming Interface, but what does that really mean? Rather than what it is, I find it easier to talk about what APIs enable. APIs can share data or functionality. For example, you might use a places API to look up restaurants by location or name, pulling out a lot of data about each place. Then you might combine that with a mapping API, using it as an interface for displaying your data.

Programmers who make a request to an API will then receive a response. For example, using our Web API to send an email, you’d pass the email contents with the request. If all goes well, you will receive a response declaring success.

What is a Webhook?

Sometimes people call webhooks reverse APIs, but perhaps more accurately a webhook lets you skip a step. With most APIs there’s a request followed by a response. No request is required for a webhook, it just sends the data when it’s available.

To use a webhook, you register a URL with the company providing the service. That URL is a place within your application that will accept the data and do something with it. In some cases, you can tell the provider the situations when you’d like to receive data. Whenever there’s something new, the webhook will send it to your URL.

“Call Me When He’s Warmed Up”

Baseball managers could really use webhooks. As the game progresses, they often want to change pitchers. To do this requires the new pitcher to first warm up in the bullpen, which is usually over 300 feet from the team’s dugout. If you watch baseball on television, you’ll often see the manager pick up a phone in the dugout. He’s making a call to the bullpen to check on the new pitcher.

  • “Is he warmed up yet?”
  • “Not yet”

Then he hangs up the phone. In a few minutes he’ll have to call again. Programmers would call this polling and it’s process-intensive for both sides. A webhook lets you say, “call me when he’s warmed up.”

Example Webhooks

SendGrid has two distinct webhooks related to each direction that email flows:

  1. Event Webhook provides data about the emails you send, such as bounces and when the recipient clicks a link.
  2. Inbound Parse Webhook allows your application to receive email as soon as a message comes in.

Some non-SendGrid webhooks:

Hopefully that gives you a few ideas about what a webhook is and how it is different from an API.

Now it’s time to start doing something with those webhooks. Here are four things you can do with webhooks, including testing them on your local machine or connecting them to other services.

【原文出處】http://www.51aras.com/?id=30
  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM