简体   繁体   English

在不发送Ruby的情况下输出原始HTTP请求

[英]Output Raw HTTP Request without Sending in Ruby

I am trying to setup a POST request to a rest api using ruby. 我正在尝试使用ruby设置对REST API的POST请求。 What I want to do is to output the raw HTTP request without actually sending the request. 我想做的是输出原始HTTP请求,而不实际发送请求。 I have looked at HTTParty and Net:HTTP, but it seems the only way to output the request is only once you send the request. 我已经看过HTTParty和Net:HTTP,但似乎输出请求的唯一方法是仅在发送请求之后。 So basically I want a convenient way for creating an HTTP request string without actually having to send it. 因此,基本上,我想要一种无需实际发送即可创建HTTP请求字符串的便捷方法。

The HTTParty.get and similar methods methods are helper functions that wraps a lot of the internal complexity; HTTParty.get和类似的方法方法是辅助函数,它包装了许多内部复杂性。 you should just peek inside the method to find that HTTParty.get to find that inside it it just makes a call to perform_request : 您应该只窥视该方法的内部以找到HTTParty.get即可在其中找到它,它只是对perform_request进行了调用

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

and peeking into perform_request, we get that it just constructs a Request object and call perform on it : 查看一下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

You should take a look into the Request class. 您应该看一下Request类。

Take a look at Typhoeus 看看Typhoeus

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

It allows you to create the request and then you can run it or not with 它允许您创建请求,然后可以通过以下命令运行或不运行

 request.run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM