Ruby - > RestClient ::资源 - >发布 - >参数

时间:2013-05-14 16:30:34

标签: ruby rest post

我构建了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

并且运行良好,但发布错误syntax error, unexpected ',', expecting ')' (SyntaxError)

任何人都可以建议我做错了什么?

1 个答案:

答案 0 :(得分: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'
            }
        )