Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart: PDF file is uploading corruptedly #689

Open
lflucasferreira opened this issue Mar 2, 2020 · 10 comments
Open

Multipart: PDF file is uploading corruptedly #689

lflucasferreira opened this issue Mar 2, 2020 · 10 comments

Comments

@lflucasferreira
Copy link

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! :)

@lflucasferreira
Copy link
Author

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

@TheSmartnik
Copy link
Collaborator

Thanks for reporting!
Does this happen with any pdf file you upload or only with a particular one?

@lflucasferreira
Copy link
Author

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”Á¶&üB�MÎ9K�”$95I$‰�ArRÉ   Ð@“s�$(YI"�É�š$HŽ’éFÉÐ-©ÉÃ93wîü8³Ö=óç[ë~»×Ú?ºê­î§j×Þû©·öíüí

@andykimchris
Copy link

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!

@TheSmartnik
Copy link
Collaborator

@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.

@alexhill113
Copy link

I am having the same issue using HTTParty 0.18.0

@TheSmartnik
Copy link
Collaborator

@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

@majorvin
Copy link

Wow.. spent good day on this. Mine is not a pdf but an image.

@majorvin
Copy link

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.

@lflucasferreira
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants