如何设置客户端“标头”和“参数”; HTTP GET 请求的值并在服务服务器中读取这些值?

发布于 2024-10-18 05:07:07 字数 1094 浏览 0 评论 0原文

我正在使用 Ruby on Rails 3,我想为客户端服务器的 HTTP GET 请求设置 headerparams 值。然后,另一方面,我想阅读服务服务器上的内容。

我在客户端中所做的是:

host = "http://<site_name>.com"
path = "/users/1.json"
query_params = ["username=test_username", "password=test_psw"].join("&")

uri = URI.parse("#{host}#{path}?#{query_params}")

http = Net::HTTP.new(uri.host, uri.port)

http.start do
  @response_test = JSON(http.get("#{host}#{path}").body)["user"]
end

我在服务中所做的是:

...
respond_to do |format|
  format.json {
    render :json => @user.to_json
    if ( params["username"] == "test_username" && password == test_psw )
      render :json => @user.to_json
    else
      render :text => "Bad request"
    end
  }
end

以上所有代码都无法正常工作:发出 HTTP GET 请求时我总是得到一个 706:“错误请求”处出现意外标记

(1) 如何在客户端中正确设置header?上例中,params设置正确吗?

(2)如何正确读取headerparams中的值服务器?

I am using Ruby on Rails 3 and I would like to set header and params values for a HTTP GET request of a client server. Then, on the other side, I would like to read those on the service server.

What I do in the client is:

host = "http://<site_name>.com"
path = "/users/1.json"
query_params = ["username=test_username", "password=test_psw"].join("&")

uri = URI.parse("#{host}#{path}?#{query_params}")

http = Net::HTTP.new(uri.host, uri.port)

http.start do
  @response_test = JSON(http.get("#{host}#{path}").body)["user"]
end

What I do in the service is:

...
respond_to do |format|
  format.json {
    render :json => @user.to_json
    if ( params["username"] == "test_username" && password == test_psw )
      render :json => @user.to_json
    else
      render :text => "Bad request"
    end
  }
end

All above code doesn't work correctly: making the HTTP GET request I get always a 706: unexpected token at 'Bad request'.

(1) How set correctly the header in the client? In the example above, are params correctly set?

(2) How to read properly header and params values in the server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2024-10-25 05:07:07

尝试解析响应后,您是否在客户端收到“706:'错误请求'时出现意外令牌”错误?如果是这样,我猜这是因为您的客户端似乎正在期待 JSON 响应,并且您正在发送未加引号的原始文本“错误请求”,该文本不会解析为有效的 JSON。尝试渲染:json => “错误请求”.to_json

在 GET 查询字符串或表单 POST 中传递的任何参数都将位于 params 哈希中。

回答您的问题:

1)您可以通过将 puts "params: #{params.inspect}" 放入控制器操作中来查看您的参数。

2) 标头可在 request.headers 哈希中找到:http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-headers

以下是如何在 Net::HTTP 请求中设置标头:

url = URI.parse("http://www.whatismyip.com/automation/n09230945.asp")

req = Net::HTTP::Get.new(url.path)
req.add_field("X-Forwarded-For", "0.0.0.0")

res = Net::HTTP.new(url.host, url.port).start do |http|
  http.request(req)
end

Are you getting the "706: unexpected token at 'Bad request'" error on the client side after attempting to parse the response? If so I would guess this is because your client appears to be expecting a JSON response, and you're sending the unquoted raw text "Bad request", which does not parse as valid JSON. Try render :json => "Bad request".to_json

Any parameters passed in a GET querystring or form POST will be in the params hash.

In answer to your questions:

1) You can see your params by putting puts "params: #{params.inspect}" inside your controller action.

2) Headers are available in the request.headers hash: http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-headers

Here's how to set headers in a Net::HTTP request:

url = URI.parse("http://www.whatismyip.com/automation/n09230945.asp")

req = Net::HTTP::Get.new(url.path)
req.add_field("X-Forwarded-For", "0.0.0.0")

res = Net::HTTP.new(url.host, url.port).start do |http|
  http.request(req)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文