如何捕获订阅回调请求

时间:2013-06-06 20:34:17

标签: google-mirror-api

我遇到了How to test Mirror API Subscriptions中解决的问题。

"我发现在本地进行开发的第二个也是最有用的方法是捕获订阅回调请求(例如从可公开访问的服务器)到日志中然后使用curl在本地/ dev机器上重现该请求"

有人可以解释如何执行此操作吗?

2 个答案:

答案 0 :(得分:1)

执行此操作的具体方式取决于您的语言和平台,但要点是您记录通知请求正文。

Java quick start实际上已在[NotifyServlet.java][2]中执行此操作。它会对请求正文进行一些快速检查,并将其记录到信息中。

public class NotifyServlet extends HttpServlet {
  private static final Logger LOG = Logger.getLogger(MainServlet.class.getSimpleName());
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // Respond with OK and status 200 in a timely fashion to prevent redelivery
    response.setContentType("text/html");
    Writer writer = response.getWriter();
    writer.append("OK");
    writer.close();

    // Get the notification object from the request body (into a string so we
    // can log it)
    BufferedReader notificationReader =
        new BufferedReader(new InputStreamReader(request.getInputStream()));
    String notificationString = "";

    // Count the lines as a very basic way to prevent Denial of Service attacks
    int lines = 0;
    while (notificationReader.ready()) {
      notificationString += notificationReader.readLine();
      lines++;

      // No notification would ever be this long. Something is very wrong.
      if(lines > 1000) {
        throw new IOException("Attempted to parse notification payload that was unexpectedly long.");
      }
    }

    LOG.info("got raw notification " + notificationString);
    JsonFactory jsonFactory = new JacksonFactory();
  ...

运行此代码后,请访问您的日志,复制JSON并将其与curl一起发布回本地服务器。

答案 1 :(得分:1)

一种可能的方法是使用nc之类的工具,它可以让您在特定端口上侦听到该端口的所有流量并显示结果(或将其重定向到文件)。 (您需要使用https / http代理,除非您擅长手动协商SSL。)

在这种情况下,服务器会向您发送将显示的回调信息,您可以复制JSON并在本地使用它。