gpt4 book ai didi

ruby-on-rails - 使用 Net::HTTP 向 HTTPS 发送请求

转载 作者:数据小太阳 更新时间:2023-10-29 08:36:20 26 4
gpt4 key购买 nike

这个使用 Ajax 的 POST 请求完美地工作:

var token = "my_token";

function sendTextMessage(sender, text) {
$.post('https://graph.facebook.com/v2.6/me/messages?',
{ recipient: {id: sender},
message: {text:text},
access_token: token
},
function(returnedData){
console.log(returnedData);
});
};

sendTextMessage("100688998246663", "Hello");

我需要有相同的请求,但在 Ruby 中。我尝试使用 Net:HTTP,但它不起作用,而且我没有收到任何错误,所以我无法调试它:

    token = "my_token"
url = "https://graph.facebook.com/v2.6/me/messages?"
sender = 100688998246663
text = "Hello"
request = {
recipient: {id: sender},
message: {text: text},
access_token: token
}.to_json


uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(uri.request_uri)

response = http.request(request)
response.body

我应该如何继续得到错误或者我哪里出错了?

最佳答案

您的request 哈希正在被您分配给Net::HTTPrequest 对象替换。还要确保在 HTTP 请求的正文中设置请求参数:

require "active_support/all"
require "net/http"

token = "my_token"
url = "https://graph.facebook.com/v2.6/me/messages?"
sender = 100688998246663
text = "Hello"
request_params = {
recipient: {id: sender},
message: {text: text},
access_token: token
}
request_header = { 'Content-Type': 'application/json' }

uri = URI.parse(url)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(uri.path, request_header)
request.body = request_params.to_json

http.request(request)

response = http.request(request)

您可能会发现以下引用有用:http://www.rubyinside.com/nethttp-cheat-sheet-2940.html

关于ruby-on-rails - 使用 Net::HTTP 向 HTTPS 发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36601942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com