gpt4 book ai didi

Ruby 发布请求 : PDF File

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:42 25 4
gpt4 key购买 nike

上传文档的API示例是这样的

headers = {
'Authorization': 'Bearer ACCESS_TOKEN',
}
params = {
'doctor': 'https://drchrono.com/api/doctors/1234',
'patient': 'https://drchrono.com/api/patients/5678',
'description': 'Short document description here',
'date': '2014-02-24',
'metatags': json.dumps(['tag1', 'tag2']),
}
with open('/path/to/your.pdf', 'rb') as f:
files = {'document': f}
requests.post(
'https://drchrono.com/api/documents',
data=params, files=files, headers=headers,
)

我使用 Prawn 创建 PDF。一条路线自动下载 PDF,而另一条路线将其呈现以在浏览器中查看。我遇到了问题所以(试图弄清楚它是 Prawn PDF 问题还是 PDF 问题)我从网上下载了一个相当基本的 PDF。同样的问题。我正在使用 HTTParty 发送我的 POST 请求。

  headers = {'Authorization' => 'Bearer ' + access_token}
File.open("#{Rails.root}/app/assets/test.pdf", "rb") do |file|
params = {
'document' => file.read,
'doctor' => 'https://drchrono.com/api/doctors/' + doctor.id,
'patient' => 'https://drchrono.com/api/patients/' + patient.id,
'description' => 'Report',
'date' => date
}
response = HTTParty.post('https://drchrono.com/api/documents', :headers => headers, :body => params)
puts response
data = JSON.parse(response.body)
puts data
end

我收到以下错误。

{"document"=>["No file was submitted. Check the encoding type on the form."]}

我最初认为也许“文档”不应该直接包含在关键文档下的正文中,但是当我注释掉我的参数“文档”时,我收到了这个错误。

{"document"=>["This field is required."]}

所以看起来它正在读取文档键并期望获得文档值但不是。如果我将 file.read 更改为 file 我会收到相同的错误

{"document"=>["No file was submitted. Check the encoding type on the form."]}

我觉得答案可能非常简单,但我已经被困了一段时间了。有什么想法吗?

最佳答案

也许您需要将其作为多部分请求发送。由于您已经在使用 HTTParty,您可能会发现 httmultiparty gem 有用。示例:

require 'httmultiparty'

class DrChronoClient
include HTTMultiParty
base_uri 'https://drchrono.com/api'
end

File.open("#{Rails.root}/app/assets/test.pdf", "rb") do |file|
headers = {
:Authorization => 'Bearer ' + access_token
}

params = {
:document => file.read,
:doctor => 'https://drchrono.com/api/doctors/' + doctor.id,
:patient => 'https://drchrono.com/api/patients/' + patient.id,
:description => 'Report',
:date => date
}

response = DrChronoClient.post('documents', :query => params, :headers => headers)
puts response
data = JSON.parse(response.body)
puts data
end

关于Ruby 发布请求 : PDF File,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24004735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com