Вопрос о том, во что превращается изображение, когда вы используете двоичное тело Postman - PullRequest
0 голосов
/ 10 июля 2020

Когда вы отправляете файл изображения с двоичным типом тела в почтальоне, как кодируется изображение при отправке или во что превращается изображение при отправке? Отправляется ли он в виде байтового массива, байтовой строки, двоичной строки или чего-то еще?

1 Ответ

0 голосов
/ 10 июля 2020

Отправляется в виде сырых байтов. Просто данные как есть. Без кодировки. Это то же самое, что и raw, но вы можете указать файл вместо текстового поля (поскольку текстовое поле уже будет ограничивать, если бы вы могли поместить в него).

Запрос в Postman:

enter image description here

Original file gif, for comparison (screenshot from HxD):

enter image description here

Data sent (screenshot from SmartSniff):

enter image description here

Text representation (screenshot from SmartSniff as well):

enter image description here

You can see that after the headers, the original GIF file is appended as-is (starting at offset 0x116 in the hex dump).


Additional Information

Addressing your comment:

Is it like a byte string?

All network communication is a stream of bits grouped into octets (bytes). I don't know what you mean by the term "byte string", but the data here is just transmitted without modification, every bit stays the same. (On a protocol (HTTP) level, that is. Of course there is all the remaining network stack below that which splits your data into packets and such.)

The Python bytes literal b'\x41\x42\x43' and the Python string 'ABC' and the bytes 41, 42 and 43 (when viewed in hexadecimal notation) in succession all represent the same thing in some way. At the end, you get a handful of zeroes and ones and those are stored/sent/whatever. (Yes it gets complicated with Non-ASCII literals but still the b'\xAB' notation is just a way to represent bytes with given values.)

would the image being sent be like b'...'?

That's sort of the wrong question, it doesn't have any meaningful answer.

Example: The letter A is normally represented by a byte with value 41 (hex - 65 in decimal), or 01000001 in binary. Those 8 bits are the information that is sent. If you write b'\x41' in Python you get the same thing - one byte with the bits 01000001. So it doesn't make sense to ask if the A is sent as b'\x41' or not, unless you mean the actual characters b'\x41' as you would type them into your code file - in that case no, because those would be 7 bytes and it doesn't make sense to do that if we could just send one byte (consisting of the value representing A) instead.

Say if I was to send the image using an http client what data format would I put the image in to send it to an api that is like sending the image with a binary body in postman?

In Python you have несколько способов для представления сырых двоичных данных, и при отправке содержимого файла вы можете просто прочитать файл в b в обычном режиме и передать данные в HTTP-запрос:

requests.post(
  url='http://httpbin.org/post',
  data=open('./image.gif', 'rb').read(),
  headers={'Content-Type': 'application/octet-stream'}
)

В node.js вы можете хранить такие данные в объекте Buffer. А в C# вы должны использовать массив байтов (Byte[]). В большинстве языков есть какой-то способ представления такой цепочки байтов без наложения на них дополнительного значения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...