httparty
httparty copied to clipboard
Multipart: PDF file is uploading corruptedly
Hi there!
I would like your help in order to find a solution to my problem, if possible.
Below is the code I am trying to make as a multipart/form-data request. The file is sent to the server, but it is always corrupted. But when I use the gem RestClient (the second code below), it works fine.
So, I believe there is an error, or with my code, or with the HTTParty gem.
HTTParty code:
class HubUploadAPI
include HTTParty
base_uri('http://10.36.11.120:8080/dsbpm-api')
headers('Content-Type': 'multipart/form-data')
def upload(token, task_id, file)
self.class.post(
'/attachment',
multipart: true, # added just to force, but even removing it, it doesn't make any difference
headers: {
'Content-Type': 'multipart/form-data', # the same as multipart description above
'X-Auth-Token': token,
Accept: '*/*'
},
body: {
'task_id': task_id,
'origin': 'PORTAL',
'file': File.open(file)
}
)
end
end
RestClient code (obs: another class file):
class HubAPI
def upload(token, task_id, file)
RestClient::Request.execute(
method: :post,
url: 'http://10.36.11.120:8080/dsbpm-api/attachment',
payload: {
'task_id': task_id,
'origin': 'PORTAL',
'file': File.open(file)
},
headers: {
'Content-Type': 'multipart/form-data',
'X-Auth-Token': token,
Accept: '*/*'
}
)
end
end
Please, I would like to keep using HTTParty as my main REST API library. I do not like mixing gems.
Thank you in advance! :)
By the way, here is the information about each gem.
HTTParty:
* httparty (0.18.0)
Summary: Makes http fun! Also, makes consuming restful web services dead easy.
Homepage: https://github.com/jnunemaker/httparty
Path: C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/httparty-0.18.0
RestClient:
* rest-client (2.1.0)
Summary: Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.
Homepage: https://github.com/rest-client/rest-client
Path: C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/rest-client-2.1.0-x64-mingw32
Thanks for reporting! Does this happen with any pdf file you upload or only with a particular one?
Thanks for reporting! Does this happen with any pdf file you upload or only with a particular one?
Any file.
The following is the content of the damaged file opened with Notepad++:
%PDF-1.4
%íáþ®
2 0 obj
<</Type /XObject /Subtype /Image
/Width 1664 /Height 2312
/ColorSpace /DeviceRGB
/BitsPerComponent 8
/Length 414975
/Filter [/FlateDecode /DCTDecode]
>>
stream
xœä»gT”Á¶&üBMÎ9K”$95I$‰ArRÉ Ð@“s$(YI"Éš$HŽ’éFÉÐ-©ÉÃ93wîü8³Ö=óç[ë~»×Ú?ºêî§j×Þû©·öíüí
Does this mean we should not use Httpaty gem for file uploads and opt for RestClient instead. Thanks @lflucasferreira for raising this issue. Love the gem!
@lflucasferreira Sorry for a late reply. I've used the code you provided and all sorts of different pdfs (very different sizes from few bytes up to 80mb and pdf versions from 1.4 to 1.6) and couldn't confirm the issue.
I am having the same issue using HTTParty 0.18.0
@alexhill113 @lflucasferreira this is super weird. As I can't confirm the issue. Can you, please, help me out in this?
Can you check this versions of httparty and see if the issue is reproduced?
- 0.16.2
- 0.16.3
- 0.17.3
If it is reproduced everywhere could you create a simple repository with server, upload script and file where this issue is reproduced.
I know that's a lot to ask, but I really want to fix this and can't do that without your help
Wow.. spent good day on this. Mine is not a pdf but an image.
What I think the problem is, we are trying to upload huge file and httparty 'stops' prematurely. Even though I added the timeout: blah, still doesnt work well.
@TheSmartnik, owing to the pandemic I am not working at the office and I am on vacation. Once I get back, I will test older versions.