tinify-php icon indicating copy to clipboard operation
tinify-php copied to clipboard

Request body of type 'application/x-www-form-urlencoded' contains invalid byte

Open kiennv58 opened this issue 4 years ago • 8 comments

Sometimes I get error: "Request body of type 'application/x-www-form-urlencoded' contains invalid byte 0xd. (HTTP 400/Bad request)" Please help me!

kiennv58 avatar Feb 19 '20 15:02 kiennv58

Answered through support. Not an API issue.

simonwahlstrom avatar Mar 03 '20 08:03 simonwahlstrom

@simonwahlstrom I'm getting this too, what's the issue and how can I fix it?

LukeTowers avatar Aug 22 '20 05:08 LukeTowers

Hi Luke,

Looking back at the communication from earlier this year, this user worked around the issue by using the fromUrl method instead of the fromBuffer method, so we don't exactly know what caused the issue.

How are you using the library? Do you consistently get this error for the same image? And if so, only for one image or all images?

mattijsvandruenen avatar Aug 24 '20 08:08 mattijsvandruenen

@warflower it's happened for six images so far, I'll need to update my error logging code to identify which ones are triggering it. I'm passing it to TinyPNG using the PHP library Source class:

$source = Tinify\Source::fromFile($tempPath);
$source->toFile($tempPath);

I've also noticed some other errors being logged:

  • Request body of type 'application/x-www-form-urlencoded' cannot be longer than 100 KB. (HTTP 413/Request entity too large)
  • File type is not supported. (HTTP 415/Unsupported media type)

What are the supported file types and is there a way to get around the 100 KB limit or is that a hard limit on the API?

LukeTowers avatar Aug 24 '20 16:08 LukeTowers

@LukeTowers we currently only support JPEG and PNG.

There is no 100 KB limit on the API. Instead, I think that the errors that you're getting are caused by attempting to compress files that aren't JPEG or PNG. For example, if I attempt to compress a .json file that's larger than 100 KB I get the exact same error as you're getting:

Request body of type 'application/x-www-form-urlencoded' cannot be longer than 100 KB.

And if I attempt a smaller .json file I get

Request body of type 'application/x-www-form-urlencoded' contains invalid byte 0xa.

Trying different files that aren't recognised as either JPEG or PNG will result in similar errors.

If you're sure that you're getting these errors for JPEG or PNG images then it would help to share an example file with us.

mattijsvandruenen avatar Aug 25 '20 09:08 mattijsvandruenen

@warflower

The invalid byte and cannot be longer than 100KB errors were occurring on webp file types, so now I'm filtering them out before sending them to be optimized, although it's strange that they didn't get Unsupported Media Type error instead.

I'm still getting Unsupported Media Type on the following images:

  • https://newcriterion.com/storage/app/media/June%202020%20blog/johnwilliamwaterhousetheremorseofneroafterthemurderofhismother.jpg
  • https://newcriterion.com/storage/app/media/June%202020%20blog/cropped-images/Shaw-Memorial-NPS-7-0-4028-1833-1592476783.jpg

LukeTowers avatar Aug 26 '20 18:08 LukeTowers

@LukeTowers I'm able to compress both those examples with the PHP library, either by downloading them locally first and using fromFile or directly from these URLs with fromUrl. There doesn't seem to be anything wrong with these images.

mattijsvandruenen avatar Aug 27 '20 08:08 mattijsvandruenen

@warflower I've updated the error logging code again so that I can get the actual files used, the TinyPNG is an optimization layer that happens after those images are resized on the server

LukeTowers avatar Aug 27 '20 17:08 LukeTowers

Closing this issue due to lack of activity. Feel free to reopen it in case you have new findings.

rkoopmans avatar Feb 20 '23 10:02 rkoopmans