我正在尝试api post请求。测试时我在rails控制台中运行它:
u = User.find(1234)
u.create_or_update_hubspot
但继续收到此消息:
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
答案 0 :(得分:6)
只需使用火箭操作符(=>
)而不是冒号操作符(:
),这样{'Key': 'Value'}
就会被{'Key' => 'Value'}
替换。在散列中使用:
运算符意味着该键是一个符号,尽管有引号,并且由于符号没有方法拆分,因此引发了错误。