Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError: undefined method `split' for :"content-type":Symbol

I am attempting an api post request. When testing I run this in rails console:

u = User.find(1234)
u.create_or_update_hubspot

but continue to get this message:

NoMethodError: undefined method `split' for :"content-type":Symbol

Any ideas on how to fix this?

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/[email protected]/?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
like image 841
Carl Warburton Avatar asked Mar 21 '17 02:03

Carl Warburton


1 Answers

Simply use rocket operator (=>) rather than colon operator (:), such that {'Key': 'Value'} is replaced by {'Key' => 'Value'}. Using the : operator in a hash implies that key is a symbol, despite of quotation, and since symbols has no method split the error is raised.

like image 74
Mr. Jaeger Avatar answered Sep 28 '22 05:09

Mr. Jaeger