0

我确实构造了 RestClient 包装器

require 'json'
require 'rest_client'

$url_common_part = 'http://host:port/cgi-bin/cgi_script'

class Grabber
    def initialize
        @site = RestClient::Resource.new($url_common_part)
    end
    def post ( path, params )
        site_and_path = @site["#{path}"]
        payload = params.to_json
        puts "\n", payload
        response = site_and_path.post (
            payload,
            headers = {
                'MY-HEADER' => 'MY-VALUE',
                'content_type' => 'json',
                'accept' => 'json'
            }
        )
        return response
    end
end

并且 get 效果很好,但是 post 给出了错误syntax error, unexpected ',', expecting ')' (SyntaxError)

有人可以建议我做错了什么吗?

4

1 回答 1

0

问题出在您发帖的电话中:

response = site_and_path.post (
            payload,
            headers = {
                'MY-HEADER' => 'MY-VALUE',
                'content_type' => 'json',
                'accept' => 'json'
            }
        )

您正在分配headers第二个参数何时是哈希(可以包括标题):

response = site_and_path.post (
            payload,
            headers: {
                'MY-HEADER' => 'MY-VALUE',
                'content_type' => 'json',
                'accept' => 'json'
            }
        )
于 2013-05-14T16:59:03.303 回答