提问人:spacerobot 提问时间:11/15/2023 最后编辑:engineersmnkyspacerobot 更新时间:11/18/2023 访问量:20
法拉第如何从POST中获取返回的参数并更新记录?
Faraday how to get returned parameter from a POST and update record?
问:
当在 rails 中创建记录时,我正在将法拉第休息帖子调用到服务。此服务将返回新创建的工单的 ticketid。然后,我想使用返回的 ticketid 更新 rails 记录。我在尝试访问返回的 ticketid 时收到错误 No implicit conversion of Hash into String I haven't able to resolve 。这是我的代码:
url = 'https://URL.com'
conn = Faraday.new(url) do |faraday|
faraday.request :json
faraday.response :json, content_type: /\bjson$/
# Adding headers
faraday.headers['apikey'] = '1234'
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['properties'] = 'ticketid'
# Capture the response within the block
response = faraday.post do |req|
req.body = requestData.to_json
end
faraday.adapter Faraday.default_adapter
if response.status == 201
ticketid = JSON.parse(response.body)['ticketid']
update(ticketid: ticketid) # Update the 'ticketid' attribute of the request record
end
end
答:
0赞
Adesoji Alu
11/18/2023
#1
好吧,您应该提供 response.body 内容,但我怀疑主要问题似乎在于您如何设置法拉第连接和处理响应。
url = 'https://URL.com'
conn = Faraday.new(url: url) do |faraday|
faraday.request :json
faraday.response :json, content_type: /\bjson$/
# Adding headers
faraday.headers['apikey'] = '1234'
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['properties'] = 'ticketid'
faraday.adapter Faraday.default_adapter
end
# Perform the POST request outside the connection setup
response = conn.post do |req|
req.body = requestData.to_json
end
# Check the response and update the record
if response.status == 201
ticketid = response.body['ticketid']
# Assuming you have a reference to the record you want to update
record.update(ticketid: ticketid)
end
评论