如何设置客户端“标头”和“参数”; HTTP GET 请求的值并在服务服务器中读取这些值?
我正在使用 Ruby on Rails 3,我想为客户端服务器的 HTTP GET 请求设置 header
和 params
值。然后,另一方面,我想阅读服务服务器上的内容。
我在客户端中所做的是:
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)如何正确读取header
和params
中的值服务器?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试解析响应后,您是否在客户端收到“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 请求中设置标头:
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-headersHere's how to set headers in a Net::HTTP request: