当您在邮递员中发送带有二进制主体类型的图像文件时,该图像在发送时如何编码或在发送时变成什么图像?它是以字节数组,字节字符串,二进制字符串或其他形式发送的吗?
它作为原始字节发送。只是数据保持原样。没有编码。它与相同,raw
但是您可以指定一个文件而不使用文本框(因为文本框已经限制了您可以放入的文本框)。
邮递员的要求:
原始文件,用于比较(HxD的屏幕截图):
发送的数据(来自SmartSniff的屏幕截图):
Text representation (screenshot from SmartSniff as well):
You can see that after the headers, the original GIF file is appended as-is (starting at offset 0x116 in the hex dump).
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?
在Python中,您有几种表示原始二进制数据的方法,发送文件的内容时,您可以使用b
inary模式读取文件并将数据传递给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[]
)。大多数语言都有某种方式来表示字节链,就像这样,还没有附加含义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句