ruby - HTTParty POST 正在发送 GET

标签 ruby github-api httparty

我正在访问 Github 的 v3 rest api ,我正在发出一个 POST 请求,试图在测试 PR 上创建评论。我收到了 200 响应,在检查请求时,它是作为 GET 而不是 POST 发送的:

response = HTTParty.post(
    "http://api.github.com/repos/my_github/my_repo/issues/1/comments",
    body: { body: "works" }.to_json,
    headers: {
      "Authorization": "Bearer #{ENV['GITHUB_TOKEN']}",
      "Content-Type": "application/json",
      "User-Agent": ENV["GITHUB_USER_AGENT"]
    }
)

response.request
=> #<HTTParty::Request:0x007fdd45a42688
 @http_method=Net::HTTP::Get,
 @last_response=#<Net::HTTPOK 200 OK readbody=true>,
 @last_uri=#<URI::HTTPS https://api.github.com/repos/my_github/my_repo/issues/1/comments>,
 @options=
  {:limit=>4,
   :assume_utf16_is_big_endian=>true,
   :default_params=>{},
   :follow_redirects=>true,
   :parser=>HTTParty::Parser,
   :uri_adapter=>URI,
   :connection_adapter=>HTTParty::ConnectionAdapter,
   :body=>{:body=>"works"},
   :headers=>
    {:Authorization=>"Bearer my_token",
     :Accept=>"application/vnd.github.machine-man-preview+json",
     :"Content-Type"=>"application/json",
     :"User-Agent"=>"me"}},
 @path=#<URI::HTTPS https://api.github.com/repos/my_github/my_repo/issues/1/comments>,

响应主体是该 PR 上所有评论的列表,这是对同一 url 的 GET 请求。我不知道为什么它不发送 POST。感谢您的帮助。

最佳答案

更改您的请求以使用 HTTPS,而不是 HTTP:

https://api.github.com/repos/my_github/my_repo/issues/1/comments

代替:

http://api.github.com/repos/my_github/my_repo/issues/1/comments

你得到:

response.request
=> #<HTTParty::Request:0x00007ffdb81c7e48 @changed_hosts=false, @credentials_sent=false, @http_method=Net::HTTP::Post ...

还有:

response.code
=> 201

关于ruby - HTTParty POST 正在发送 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53891933/

相关文章:

arrays - 如何将子数组添加到自身? ruby

ruby - 尝试使用 ruby​​ HTTParty 发送删除

javascript - 无法下载远程 https svg 文件但嵌入 <img> 内很好

ruby - 如何让 Ruby 方法显示它们期望的类型

ruby - 尝试使用 RVM 安装 Ruby 时运行 '__rvm_make -j8' 时出错

ruby - 如何从文件创建IO对象?

用于创建文件的 GitHub API

ruby - Github API 响应 'Content is not valid Base64'

curl - Github API 错误 - { "message": "Not Found", "documentation_url": "https://developer.github.com/v3"} 在 cURL 终端中制作 repo

ruby-on-rails - 使用 rails 来使用 web 服务/api