簡體   English   中英

在不發送Ruby的情況下輸出原始HTTP請求

[英]Output Raw HTTP Request without Sending in Ruby

我正在嘗試使用ruby設置對REST API的POST請求。 我想做的是輸出原始HTTP請求,而不實際發送請求。 我已經看過HTTParty和Net:HTTP,但似乎輸出請求的唯一方法是僅在發送請求之后。 因此,基本上,我想要一種無需實際發送即可創建HTTP請求字符串的便捷方法。

HTTParty.get和類似的方法方法是輔助函數,它包裝了許多內部復雜性。 您應該只窺視該方法的內部以找到HTTParty.get即可在其中找到它,它只是對perform_request進行了調用

def get(path, options={}, &block)
  perform_request Net::HTTP::Get, path, options, &block
end

查看一下perform_request,我們得到它只是構造了一個Request對象,並對其執行調用

def perform_request(http_method, path, options, &block) #:nodoc:
  options = default_options.merge(options)
  process_headers(options)
  process_cookies(options)
  Request.new(http_method, path, options).perform(&block)
end

您應該看一下Request類。

看看Typhoeus

request = Typhoeus::Request.new(
  "www.example.com",
  method: :post,
  body: "this is a request body",
  params: { field1: "a field" },
  headers: { Accept: "text/html" }
)

它允許您創建請求,然后可以通過以下命令運行或不運行

 request.run

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM