问题描述
上传文件的API的例子是这样的。
The API example for uploading documents is this
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,
)
我用大虾来创建一个PDF文件。而另一个使得它在浏览器中查看一个路由自动下载PDF文件。我是运行到这样的问题(要揣摩它是否是一个虾PDF问题或PDF的问题)我下载一个PDF掉,这是相当基本的网。同样的问题。我使用HTTParty送我POST请求。
I use Prawn to create a PDF. One route automatically downloads the PDF while the other renders it to be viewed in the browser. I was running into issues so (to try to figure out whether it was a Prawn PDF issue or a PDF issue) I downloaded a PDF off the net that was rather basic. Same issues. I'm using HTTParty to send my POST request.
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
我收到以下错误。
I receive the following error.
{"document"=>["No file was submitted. Check the encoding type on the form."]}
我最初以为也许文件是不应该直接在关键文档下包含在身上,但是当我注释掉我PARAMS文件,然后我收到此错误。
I initially thought perhaps "document" wasn't supposed to be included in the body directly under the key document, but when I commented out my params "document" then I received this error.
{"document"=>["This field is required."]}
所以看起来它是阅读的文档键和期望得到的文档的价值,但并非如此。如果我改变 file.read
只是文件
我收到了同样的错误。
So it seems it is reading the document key and expecting to get a document value but isn't. If I change file.read
to just file
I receive the same error
{"document"=>["No file was submitted. Check the encoding type on the form."]}
我觉得答案很可能是超简单的,但我一直在坚持了一段时间了。任何想法?
I feel like the answer is probably extra-simple, but I've been stuck for a while now. Any ideas?
推荐答案
也许你需要将其发送的多部分请求。既然你已经使用HTTParty,你可能会发现 httmultiparty
一>宝石有用。例如:
Maybe you need to send it as a multipart request. Since you are already using HTTParty, you may find the httmultiparty
Gem useful. Example:
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
这篇关于红宝石POST请求:PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!