NoMethodError:未定义的方法`split'for:"content-type":Symbol

Car*_*ton 4 ruby-on-rails hubspot

我正在尝试api post请求.测试时我在rails控制台中运行:

u = User.find(1234)
u.create_or_update_hubspot
Run Code Online (Sandbox Code Playgroud)

但继续得到这个消息:

NoMethodError:未定义的方法`split'for:"content-type":Symbol

有想法该怎么解决这个吗?

def create_or_update_hubspot

    require 'net/http'
    require 'uri'
    require 'json'

    hubspot_api = 'b193b89b-0ff1-40c6-a428-b7327f3bc430'

    uri = URI.parse("https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/testingapis@hubspot.com/?hapikey=#{hubspot_api}")

    header = {'Content-Type': 'application/json'}
    user = {"Properties":[
        {
            "property": "First Name",
            "value": "user.first_name"
        },
        {
            "property":"Last Name",
            "value":"user.last_name"
        },
        {
            "property": "Email",
            "value": "user.email"
        },
        {
            "property":"Mobile Phone Number",
            "value":"user.phone_number"
        },
        {
            "property":"Microsite",
            "value": "user.tags"
        },
        {
            "property":"Company Plan",
            "value":"user.plan"
        },
        {
            "property":"Source?",
            "value":"user.registration_source"
        }
      ]
    }

# Create the HTTP objects
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri, header)
    request.body = user.to_json

# Send the request
    response = http.request(request)

  end
Run Code Online (Sandbox Code Playgroud)

小智 7

只需使用rocket operator(=>)而不是冒号运算符(:),这样就{'Key': 'Value'}可以替换为{'Key' => 'Value'}.:在散列中使用运算符意味着该键是一个符号,尽管有引号,并且由于符号没有方法拆分,因此引发了错误.