我遇到了Github的v3其余版本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。任何帮助表示赞赏。
答案 0 :(得分:3)
将您的请求更改为使用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