gpt4 book ai didi

ruby-on-rails - 如何在 Rails 中使用 x-www-form-urlencoded

转载 作者:数据小太阳 更新时间:2023-10-29 06:52:37 24 4
gpt4 key购买 nike

我正在尝试从 ExactOnlineAPI 访问 token 但文档建议仅使用 x-www-form-urlencoded。 Ruby on Rails 是否有这种编码,如果有我该如何使用它。

x-www-form-urlencodedencode_www_form 有什么区别

 params =  {
:code => "#{code}",
:redirect_uri => '/auth/exact/callback',
:grant_type => "authorization_code",
:client_id => "{CLIENT_ID}",
:client_secret => "CLIENT_SECRET"
}
uri = URI('https://start.exactonline.nl/api/oauth2/token')
#
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts "Access Token: "+res.body

最佳答案

Request bodies are defined by a form’s markup. In the form tag there is an attribute called enctype, this attribute tells the browser how to encode the form data. There are several different values this attribute can have. The default is application/x-www-form-urlencoded, which tells the browser to encode all of the values.

所以当我们想要发送数据以通过这些数据作为表单的参数提交表单时, header 将发送 application/x-www-form-urlencoded 以定义 enctype

http.set_form_data(param_hash)

为了你的

params =  {
:code => "#{code}",
:redirect_uri => '/auth/exact/callback',
:grant_type => "authorization_code",
:client_id => "{CLIENT_ID}",
:client_secret => "CLIENT_SECRET"
}
uri = URI('https://start.exactonline.nl/api/oauth2/token')
#

Net::HTTP::Get.new(uri.request_uri).set_form_data(params)

或者对于表单提交的 post 请求,使用 Net::HTTP::Post

encode_www_form是:

它从给定的枚举生成 URL 编码的表单数据。

URI.encode_www_form([["name", "ruby"], ["language", "en"]])
#=> "name=ruby&language=en"

你的情况

uri.query = URI.encode_www_form(params)
#=> "code=aas22&redirect_uri=...."

更多信息 Here

关于ruby-on-rails - 如何在 Rails 中使用 x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34203825/

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