Topics

uploaded pictures are in a bad shape

Fernando Usero <fusero@...>
 

Hi guys,

I am having a really weird beahaviour and I am bit frustrated with this because I cannot see what more to test.

I am uploading a jpg pic to parse.com and when downloading it, it never never is recognized as a jpg file. It seems to be the jpg file with some text placed at the beginning:

<<<<
--704472
Content-Disposition: form-data; name="upload[file]"; filename="yo.jpg"
Content-Type: image/jpeg
>>>>

I am using this restclient command:

<<<<
RestClient.post 'https://api.parse.com/1/files/yo.jpg',
{ :upload => { :file =>File.new('yo.jpg') } },
{ :content_type => "image/jpg", :"X-Parse-Application-Id" => XXXX, :"X-Parse-REST-API-Key"=> YYYY }
>>>>>
However using the translation to curl it works smoothly and the jog file can be downloaded again:

<<<<
curl -X POST -H "X-Parse-Application-Id: XXXX"  -H "X-Parse-REST-API-Key: YYYY" -H "Content-Type: image/jpeg" --data-binary '@yo.jpg' https://api.parse.com/1/files/yo.jpg
>>>>>

i tried several things:

- Trying to use IO.read instead of File.new (read in this mailing list)
- Tryin to change to content-type tp image/jpg instead of image/jpg
- Trying to set multipart => true and false

Any hint? Any clue?

Lennon Day-Reynolds <rcoder@...>
 

I think your problem here is using the form encoding of the payload, not anything related to the content type or use of IO vs. File.

What happens if you try something like the version below? (I would try it myself, but I don't actually use/have access to Parse.)

---
open('yo.jpg') do |fh|
RestClient.post(
'https://api.parse.com/1/files/yo.jpg', fh,
{ :content_type => 'image/jpeg', :'X-Parse-Application-Id' => '…', :'X-Parse-REST-API-Key' => '…' }
)
end
---

From my reading of the current RestClient source, it looks like the above (passing the payload as a string, rather than a hash of form field values) should directly pass the raw image data, instead of encoding it as an HTML form. (Also, note the block form of opening the file; your version would leak open file handles if it ran in a persistent context like a Rails controller, due to never closing the opened File instance.)

<api-design-rant>As an aside: WTF is up with using a POST to a fixed resource URL as a means to do a file upload? Parse supports PUT elsewhere in their API, and that seems like a strictly better approach to use here, as well.</api-design-rant>

Hope that helps,

Lennon

--
Lennon Day-Reynolds
rcoder@... //@rcoder

On Sunday, December 9, 2012 at 3:02 PM, Fernando Usero wrote:
Hi guys,

I am having a really weird beahaviour and I am bit frustrated with this because I cannot see what more to test.

I am uploading a jpg pic to parse.com (http://parse.com) and when downloading it, it never never is recognized as a jpg file. It seems to be the jpg file with some text placed at the beginning:

<<<<
--704472
Content-Disposition: form-data; name="upload[file]"; filename="yo.jpg"
Content-Type: image/jpeg

I am using this restclient command:

<<<<
RestClient.post 'https://api.parse.com/1/files/yo.jpg',
{ :upload => { :file =>File.new('yo.jpg') } },
{ :content_type => "image/jpg", :"X-Parse-Application-Id" => XXXX, :"X-Parse-REST-API-Key"=> YYYY }
However using the translation to curl it works smoothly and the jog file can be downloaded again:

<<<<
curl -X POST -H "X-Parse-Application-Id: XXXX" -H "X-Parse-REST-API-Key: YYYY" -H "Content-Type: image/jpeg" --data-binary '@yo.jpg' https://api.parse.com/1/files/yo.jpg

i tried several things:

- Trying to use IO.read instead of File.new (read in this mailing list)
- Tryin to change to content-type tp image/jpg instead of image/jpg
- Trying to set multipart => true and false

Any hint? Any clue?

Fernando Usero <fusero@...>
 

Hi Lennon, thanks for your hint. It didn't work. I got this error using your approach:

RestClient::ServerBrokeConnection: Server broke connection:
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:182:in `rescue in transmit'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:140:in `transmit'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
    from (irb):167:in `block in irb_binding'

BTW, first thing after fixing this I will handle the file descriptors that are left open.....

any idea?


On Mon, Dec 10, 2012 at 12:51 AM, Lennon Day-Reynolds <rcoder@...> wrote:
I think your problem here is using the form encoding of the payload, not anything related to the content type or use of IO vs. File.

What happens if you try something like the version below? (I would try it myself, but I don't actually use/have access to Parse.)

---
open('yo.jpg') do |fh|
RestClient.post(
'https://api.parse.com/1/files/yo.jpg', fh,
{ :content_type => 'image/jpeg', :'X-Parse-Application-Id' => '…', :'X-Parse-REST-API-Key' => '…' }
)
end
---

From my reading of the current RestClient source, it looks like the above (passing the payload as a string, rather than a hash of form field values) should directly pass the raw image data, instead of encoding it as an HTML form. (Also, note the block form of opening the file; your version would leak open file handles if it ran in a persistent context like a Rails controller, due to never closing the opened File instance.)

<api-design-rant>As an aside: WTF is up with using a POST to a fixed resource URL as a means to do a file upload? Parse supports PUT elsewhere in their API, and that seems like a strictly better approach to use here, as well.</api-design-rant>

Hope that helps,

Lennon

--
Lennon Day-Reynolds
rcoder@... // @rcoder


On Sunday, December 9, 2012 at 3:02 PM, Fernando Usero wrote:

> Hi guys,
>
> I am having a really weird beahaviour and I am bit frustrated with this because I cannot see what more to test.
>
> I am uploading a jpg pic to parse.com (http://parse.com) and when downloading it, it never never is recognized as a jpg file. It seems to be the jpg file with some text placed at the beginning:
>
> <<<<
> --704472
> Content-Disposition: form-data; name="upload[file]"; filename="yo.jpg"
> Content-Type: image/jpeg
> > > >
> > >
> >
>
>
> I am using this restclient command:
>
> <<<<
> RestClient.post 'https://api.parse.com/1/files/yo.jpg',
> { :upload => { :file =>File.new('yo.jpg') } },
> { :content_type => "image/jpg", :"X-Parse-Application-Id" => XXXX, :"X-Parse-REST-API-Key"=> YYYY }
> > > > >
> > > >
> > >
> >
>
> However using the translation to curl it works smoothly and the jog file can be downloaded again:
>
> <<<<
> curl -X POST -H "X-Parse-Application-Id: XXXX" -H "X-Parse-REST-API-Key: YYYY" -H "Content-Type: image/jpeg" --data-binary '@yo.jpg' https://api.parse.com/1/files/yo.jpg
> > > > >
> > > >
> > >
> >
>
>
> i tried several things:
>
> - Trying to use IO.read instead of File.new (read in this mailing list)
> - Tryin to change to content-type tp image/jpg instead of image/jpg
> - Trying to set multipart => true and false
>
> Any hint? Any clue?


Charles S van der Linden <sqapro@...>
 

I might suggest you consider sending the traffic through something like fiddler or wire shark, that allows you to see exactly what got sent.

On Dec 10, 2012 1:07 PM, "Fernando Usero" <fusero@...> wrote:
Hi Lennon, thanks for your hint. It didn't work. I got this error using your approach:

RestClient::ServerBrokeConnection: Server broke connection:
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:182:in `rescue in transmit'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:140:in `transmit'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
    from (irb):167:in `block in irb_binding'

BTW, first thing after fixing this I will handle the file descriptors that are left open.....

any idea?


On Mon, Dec 10, 2012 at 12:51 AM, Lennon Day-Reynolds <rcoder@...> wrote:
I think your problem here is using the form encoding of the payload, not anything related to the content type or use of IO vs. File.

What happens if you try something like the version below? (I would try it myself, but I don't actually use/have access to Parse.)

---
open('yo.jpg') do |fh|
RestClient.post(
'https://api.parse.com/1/files/yo.jpg', fh,
{ :content_type => 'image/jpeg', :'X-Parse-Application-Id' => '…', :'X-Parse-REST-API-Key' => '…' }
)
end
---

From my reading of the current RestClient source, it looks like the above (passing the payload as a string, rather than a hash of form field values) should directly pass the raw image data, instead of encoding it as an HTML form. (Also, note the block form of opening the file; your version would leak open file handles if it ran in a persistent context like a Rails controller, due to never closing the opened File instance.)

<api-design-rant>As an aside: WTF is up with using a POST to a fixed resource URL as a means to do a file upload? Parse supports PUT elsewhere in their API, and that seems like a strictly better approach to use here, as well.</api-design-rant>

Hope that helps,

Lennon

--
Lennon Day-Reynolds
rcoder@... // @rcoder


On Sunday, December 9, 2012 at 3:02 PM, Fernando Usero wrote:

> Hi guys,
>
> I am having a really weird beahaviour and I am bit frustrated with this because I cannot see what more to test.
>
> I am uploading a jpg pic to parse.com (http://parse.com) and when downloading it, it never never is recognized as a jpg file. It seems to be the jpg file with some text placed at the beginning:
>
> <<<<
> --704472
> Content-Disposition: form-data; name="upload[file]"; filename="yo.jpg"
> Content-Type: image/jpeg
> > > >
> > >
> >
>
>
> I am using this restclient command:
>
> <<<<
> RestClient.post 'https://api.parse.com/1/files/yo.jpg',
> { :upload => { :file =>File.new('yo.jpg') } },
> { :content_type => "image/jpg", :"X-Parse-Application-Id" => XXXX, :"X-Parse-REST-API-Key"=> YYYY }
> > > > >
> > > >
> > >
> >
>
> However using the translation to curl it works smoothly and the jog file can be downloaded again:
>
> <<<<
> curl -X POST -H "X-Parse-Application-Id: XXXX" -H "X-Parse-REST-API-Key: YYYY" -H "Content-Type: image/jpeg" --data-binary '@yo.jpg' https://api.parse.com/1/files/yo.jpg
> > > > >
> > > >
> > >
> >
>
>
> i tried several things:
>
> - Trying to use IO.read instead of File.new (read in this mailing list)
> - Tryin to change to content-type tp image/jpg instead of image/jpg
> - Trying to set multipart => true and false
>
> Any hint? Any clue?



Fernando Usero <fusero@...>
 

Finally solution was switching from Rest-client to NET::HTTP


On Tue, Dec 11, 2012 at 5:01 AM, Charles S van der Linden <sqapro@...> wrote:

I might suggest you consider sending the traffic through something like fiddler or wire shark, that allows you to see exactly what got sent.

On Dec 10, 2012 1:07 PM, "Fernando Usero" <fusero@...> wrote:
Hi Lennon, thanks for your hint. It didn't work. I got this error using your approach:

RestClient::ServerBrokeConnection: Server broke connection:
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:182:in `rescue in transmit'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:140:in `transmit'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
    from /home/fusero/.rvm/gems/ruby-1.9.2-p320@xserver/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
    from (irb):167:in `block in irb_binding'

BTW, first thing after fixing this I will handle the file descriptors that are left open.....

any idea?


On Mon, Dec 10, 2012 at 12:51 AM, Lennon Day-Reynolds <rcoder@...> wrote:
I think your problem here is using the form encoding of the payload, not anything related to the content type or use of IO vs. File.

What happens if you try something like the version below? (I would try it myself, but I don't actually use/have access to Parse.)

---
open('yo.jpg') do |fh|
RestClient.post(
'https://api.parse.com/1/files/yo.jpg', fh,
{ :content_type => 'image/jpeg', :'X-Parse-Application-Id' => '…', :'X-Parse-REST-API-Key' => '…' }
)
end
---

From my reading of the current RestClient source, it looks like the above (passing the payload as a string, rather than a hash of form field values) should directly pass the raw image data, instead of encoding it as an HTML form. (Also, note the block form of opening the file; your version would leak open file handles if it ran in a persistent context like a Rails controller, due to never closing the opened File instance.)

<api-design-rant>As an aside: WTF is up with using a POST to a fixed resource URL as a means to do a file upload? Parse supports PUT elsewhere in their API, and that seems like a strictly better approach to use here, as well.</api-design-rant>

Hope that helps,

Lennon

--
Lennon Day-Reynolds
rcoder@... // @rcoder


On Sunday, December 9, 2012 at 3:02 PM, Fernando Usero wrote:

> Hi guys,
>
> I am having a really weird beahaviour and I am bit frustrated with this because I cannot see what more to test.
>
> I am uploading a jpg pic to parse.com (http://parse.com) and when downloading it, it never never is recognized as a jpg file. It seems to be the jpg file with some text placed at the beginning:
>
> <<<<
> --704472
> Content-Disposition: form-data; name="upload[file]"; filename="yo.jpg"
> Content-Type: image/jpeg
> > > >
> > >
> >
>
>
> I am using this restclient command:
>
> <<<<
> RestClient.post 'https://api.parse.com/1/files/yo.jpg',
> { :upload => { :file =>File.new('yo.jpg') } },
> { :content_type => "image/jpg", :"X-Parse-Application-Id" => XXXX, :"X-Parse-REST-API-Key"=> YYYY }
> > > > >
> > > >
> > >
> >
>
> However using the translation to curl it works smoothly and the jog file can be downloaded again:
>
> <<<<
> curl -X POST -H "X-Parse-Application-Id: XXXX" -H "X-Parse-REST-API-Key: YYYY" -H "Content-Type: image/jpeg" --data-binary '@yo.jpg' https://api.parse.com/1/files/yo.jpg
> > > > >
> > > >
> > >
> >
>
>
> i tried several things:
>
> - Trying to use IO.read instead of File.new (read in this mailing list)
> - Tryin to change to content-type tp image/jpg instead of image/jpg
> - Trying to set multipart => true and false
>
> Any hint? Any clue?