我正在尝试一个 api post 请求。测试时,我在 rails 控制台中运行它:
u = User.find(1234)
u.create_or_update_hubspot
但继续收到此消息:
NoMethodError: 未定义的方法"拆分"为:"内容类型":符号
关于如何解决这个问题的任何想法?
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
只需使用火箭运算符(=>
(而不是冒号运算符(:
(,这样{'Key': 'Value'}
就会被{'Key' => 'Value'}
取代。在哈希中使用 :
运算符意味着键是一个符号,尽管有引号,并且由于符号没有方法拆分,因此会引发错误。