I have the following curiosity. If I make a request through TCP/IP to a server, it returns a code that I don't understand what it is or how I can save it.
Summarizing my question: I want to save the response from the server in jpg format , but putting the code that it sends me in a file with a jpg extension does not work (I have copied and pasted it exactly as it arrived). I have made the request through netcat . I'm not looking to save the image with wget or anything, I'd just like to understand what this weird code the server sends me is and why I can't save it to a file and have it work.
I have made the request to the following image:
http://kaboompics.com/files/upload/o_1aqd50lvlpd71hgv10361op4vdk7_new.jpg
This is my request:
GET /files/upload/o_1aqd50lvlpd71hgv10361op4vdk7_new.jpg HTTP/1.1
Host: kaboompics.com
Accept: */*
And this is what I get from the server(response):
HTTP/1.1 200 OK
Cache-Control: public, max-age=2592000
Expires: Sun, 05 Feb 2017 10:39:27 GMT
ETag: "51129-57b4d237-7f06ee67a312ca10"
Last-Modified: Wed, 17 Aug 2016 21:08:07 GMT
Content-Type: image/jpeg
Content-Length: 332073
Date: Fri, 06 Jan 2017 10:39:27 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: close
bodyresponse:
����JFIF��<CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 100
��C��C��,"��
���}!1AQa"q2���#B��R��$3br�
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������
���w!1AQaq"2�B���� #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?��l��Ք��up(s�6�$����1����_5<�H��\E�p&L��s�-���k
i�̑�*�B�89m�xq�3���mXK��+�mPNb� �1�����$�lc�M�/U���4����8ͻ0�G�NH!��np8�I����ח�'�&�##�������$
�b�s�,��ݰ��B�������zц��cر����:�F� ��rWv���F����_�;b�!Fd��d�dr3���
�$��xk� VU�el'$�I �l�0;k*�P�D�����y���7ul3�����,b�I� �BN`�d��嬹����r���������4���gRʬ,�W���a�y� ����|���[sʐE�*�P��3*����$
Đ�ڝ��a!�6�����a� ��8rMq�RZ�һ���s��,�O��z���-���FSQo�ۯ��+�v�����>v��##���*dA�
}��{�BDأ
���B��;�o� ��Q��>�M ��6�x��z�������6ua��|N��墶D(�(�VR�mQ����9�ɵ6ݬ������f��+���� ?C�6��g�ʳ�v�˓�pNk[יʹ�t_���'.%H' �]���̑ɱB�@
�ʏ���
�9�9TX�ֲ�v7vHPcnܪ�*��[���|�8�P:`���M��n6�Եwz��BI?1��\��N9�8$�Yu�$�#�V�v�@��/�@-������.���;*�bU`W8ݴn��e��@ ��WR�R�P�gH�q��� ��2���
�>ﶿ���k����F��E�C"��X���pN0z��%j�����bv��a��+� ��� #�� �m]�X�9a���2ų܀@Q��/ c���aE����}�I �ŕ�9C����Q($�W��y�3�W����: .�y0�� t�������N2�0˾����ɵ�V ��pK�NT��UIukqfUV��s�n*I�\�V-�g��j�/w�c�o��C ���c����0��J߁R��uwo��\x�+yR��.�Å]�,p�
��U$�&���e�)#C.N =nr� yn���v�^>��;C��nr�T�?/
I��T$1=��l���
\�n�Ǣ��@\7TU���ߡ��;�cԯ�H���ݜ�و`�6���$oe�*���]�g,CeF�)�w#vYF�z���>Y%�`']ʹ1�H,݉ �o��#����,����!�;�0*U@^�mb��i�;l����̞g}�o#�㼔ͼH=ξc��QA���pޤ�����$��,��GP�R�[;�$���Am�I��B !���?)dm�7d�!��� �HC1gR����>W%�$���MK����A��_��zp�UeU26���y�@8Tp V�ĮjC~d����ew.�p��͂K|��������8P�2 b�ź��]�v����O#�쵈�I�L�m%���̅R�
����8+�ky~cRk���;��H��60�d��ّ���� �������\�I*�9S�8 (�����ԮO���`��:P��(������2!�8l��NH]ʸ\ORB����\/�H�5����
�جľ��c9V,� �p8䘚�c�Am�)' �,�������Y�n%��3Мc���l��s�I`j��znPA�8PXm|�~\��s�3�Vv-j�hI1_�}�!VS��q�%r�X��U�fS��s��2������e����5�Wl��NH<1��A�$�'ni5���y�'*0H'?9bpr@�x c4Y�����W�>R]�� <��q�Ae'>��"��Yv��� R�`r3���9"�K}�3 ��bU��]��g$�n�FH�k0+�aU�,2�%�f\.8\m�T�2M
5� /�@$���*v+�H�:ڤ�@�L��r�Uf��V.�v���n#8m�p��!rp�c��T�灖;�N�Y�fw��T�����;��� R�\��b���pX(%q��N�z�20A3t�ueu. ,���p@}����2��# QΫr��<�R2pK89
�˒P�KH$@K)S���ew�8*H+���4Y��g��/%��71�B�hP�+��@�韙?0�0Y.�U?:� -�_�F�d�%��1�`�^lߠ,3��@��,9� ���߶��v1�[�"���v�A!Nr����nIo�u:�~��r6���@�3��d�
��T���cnI�s�J��$�e',9J_�)R�pp� $�r���1�9Q�F'R��D!r�'s�9<+(��J�r�y�,��ԓW�θ]�`��T���a�8<�p��#f�h��nU?1�As��,F���prEq1ݙfp�c���X��G`pAi4�������3��!�큲H���B�{!�����[xa?�\��I8 r0HS�B�9I@ܧ9ry<�6���$�N�I�����d�c�`8�FN�� =3���g1H���3�Ӟh'$n��#�[����?����>�NN�I �0��Ċ�����ހd��6�RWkg�q�9|�0$�vo�*>a�H��\|���1�� ,o��ml�ey bH��
F���Z�Y�s�;-�T��6��$�A�U+��6�\d��f#8�
�q��AA�sή��Yv�fffR+�L���T�Mi�(eT��RT�ݥq�>���!r2]�g�f�� �3�1;dÜ���e3����-}v��ڸ����~m��,�+�+Uo��)c�"���v�������#���79�>���9�2O����@\rARWh��c�'
1�K��_�}țkM��_��&���0��s'�ӌs��
J�PY����ڣ��A���U?���b�y�X����\����q g�r:�����1�(UrF���C���0y�$i�^���2�Mǝ3��n^�d���z��6I=v���0Pp�a���n$�*��*@�*v�p
b�p�K�����c���<�q��n+�p��� HTd��3�q��
�)ǶA;)[���U�P�N�ۘ��z|�0(?�ӓ��*^B#)A����6ܜa@$arI=FT1�MD���B�u�$2A�+���'md�kbM�x;�.m���=��Oഝ�Ore$ս�Ȯ|���G.��\��B�a��M�F<���*L������ ���0X��0������
�=� ����/!wQ� +�V��8�ۓ��q�mV �r�݀2ē�1���A�n�n���a.]}�~�3gGi
�T�Ai�lwI��gj� �ug\�2��]�z9V��X&��O�8@�)q�H��Z2d+����E�G@�'��p.�ݷ"���%2�P���.��'5�#/�ha)�'w�z�[\.����*T�*��<��,�8ub �V��@������@wVbp������|��6�o���˴����2Jc�e*+:k��M�y�VC��*
EDIT:
I have spent several days with this doubt and I don't know if I will finally solve it. With Trauma's comment I have passed everything through a hexadecimal editor , leaving the result as follows:
1º Descarga de la imagen original vía web
2º Realización de un request a la imagen vía la web **requestmaker**
3º Realización de un request a la imagen vía **netcat**
From the results I get the following:
1st The original image (saved as image.jpg) occupies 332,073 bytes (325 KiB) on disk . The body that requestmaker gives me is 86,668 bytes (85 KiB) and the response that netcat gives me to the request (including headers and body) is 331,355 bytes (324 KiB) , by deleting the headers and leaving only the body of the file overall size does not vary much.
As you can see this, it makes you think that something is not right because none of the three coincide.
2nd I pass all three through a hexadecimal editor, with the result that both the image.jpg and the request made through the web appear in legible text, somewhat identifying the type of file because the word JPG and the word QUALITY=100 appear . In the file created with netcat directly it is all hexadecimal without readable conversion (all symbols). The only file of the three that can be opened as an image is logically the first downloaded from the web, the others are useless. In addition, the image.jpg file is the only one that has the hexadecimal characters that correspond to an image (FF D8 FF E0)
I can't think of what else I can do.
EDIT 2:
After continuing with the tests one day without knowing the reason, it already works for me. I haven't (consciously) changed anything so I don't know what it could be. When downloading the image with netcat it comes down with the correct size and the correct format. Now I can see it
The response from the server tells you all the data you need:
Indicates that it sends you a file in jpeg format.
Indicates the length of the file, in bytes.
The server will close the connection, not waiting for new requests.
The image you indicate, if you check its size, you will see that it is exactly 332073 bytes .
The header does not indicate any other relevant data, such as the compression used.
The server is sending you exactly what you expect, an image. It would be enough to do what you indicate, save the
BODY
and rename it. Your schematic is correct.I have no experience with
netcat
, so I can't tell you how to do it. Remember not to open the file with a text editor, but with an editor that supports binary data. And that the header is separated from the data by 2 line breaks.EDIT
The [RequestMaker][1] site is not intended for binary responses; the text box in which you display the result is filled with characters
?
, which usually indicates that errors or problems with the encoding of the text are occurring; this is a consequence of binary data; the site tries to interpret them as text, with some encoding (probably expecting UTF-8).The UTF-8 encoding has certain rules, which do not apply to binary data. Certain bytes have special values, and not all combinations of bytes are allowed . Those errors are represented by that character
?
. And data will be missing; those sequences of more than 1 byte displayed as a single byte?
imply that the result offered by the page is smaller than the real result.EDIT 2
After installing the
netcat
, and following exactly your stepsI get a file of 332406 bytes. If we list its first 13 lines:
Note that the graphic file, the downloaded data, starts right at line 13 (line 12 is blank, but relevant).
This number, the 12 lines, will vary from server to server (and may even vary from request to request). Remember that the headers are separated from the data by a blank line that will always be present .
Now if we discard those first 12 lines
We get the end result, a 332073 byte file .
EDIT 3
We can find line endings as well
\n
as\r\n
, depending on the server. This can cause us some trouble to automate the process of removing the headers from the body .this command
It takes this into account, and eliminates all the headers; removes lines, until it finds a blank one, regardless of how the line endings are encoded.