Product developmentManual web testingAPI

Payloads

8 minutes read

In the world of web development, it's essential to understand payloads. A payload is the data sent with an HTTP request or response. For example, in an HTTP POST request, the payload would hold form data, JSON, XML, or any different type of data you're sending to the server.

HTTP protocol body

The payload includes different types of information, like user input, configuration settings, API requests and responses, database records, files, and multimedia content.

  • In web development, the payload might be form data a user has submitted, the JSON or XML data an API has exchanged, or the HTML content a server has rendered and shown in the user's browser.

  • In a mobile app, the payload can include user interactions, device sensor data, location information, push notifications, and data from remote servers.

  • In desktop software, the payload can have user input, database queries, file operations, and communication with other software components.

  • In API work, the payload is the data the client and server swap, including request parameters, request bodies, headers, and response data.

The HTTP message body has the payload, which might be text, binary data, or other content types that the client and server swap. The payload in an HTTP message body comes in various formats like plain text, JSON, XML, HTML, binary data, or multipart content. What format the payload has depends on the data's nature and the client and server applications' needs.

The Content-Length header in the HTTP message header shows the payload's size in bytes. This header lets the recipient know the data amount to expect in the message body and makes data sending and processing more efficient. The Content-Type header in the HTTP message header tells the recipient the payload data's type and format. It informs the recipient about the correct way to understand and handle the payload content.

Consider data such as:

{
  "name": "John Doe",
  "age": 30,
  "email": "[email protected]",
  "is_active": true
  }

We can see the same example in XML format:

<user>
  <name>John Doe</name>
  <age>30</age>
  <email>[email protected]</email>
  <is_active>true</is_active>
</user>

And also in plain text:

Name: John Doe
Age: 30
Email: [email protected]
Is Active: true

The payload gives us information about a person named John Doe. It has fields like name, age, email, and is_active (a true or false value that shows if the person's account is active). You can use this payload in different situations, like sending user data in an HTTP request body, saving information in a database, or swapping data between systems through an API.

HTTP body content type

The HTTP body content type, also known as the Content-Type header, is a key part of the Hypertext Transfer Protocol (HTTP) message structure. It tells you the type and format of the data in the HTTP message body. The Content-Type header shows you how to read and handle the payload correctly.

Here's a breakdown of the HTTP body content type:

  1. The Content-Type header is among the HTTP message headers and shows the media type of the resource's body. You will see it as "Content-Type: [media type]" in the HTTP headers.

  2. The media type in the Content-Type header explains the nature and format of the data in the HTTP message body. Usual media types include text/plain, application/json, application/xml, text/html, image/jpeg, and application/octet-stream (for any binary data).

  3. The Content-Type header has a clear syntax: "type/subtype". For instance, "text/html" means the content is text in HTML format, while "application/json" tells you it's JSON-formatted data.

  4. Sometimes, the Content-Type header can have a charset parameter to show the character encoding of the body. For instance, "Content-Type: text/html; charset=utf-8" means the HTML content uses the UTF-8 character encoding.

  5. In HTTP requests, the Content-Type header tells the format of the data sent from the client to the server (like in POST requests with form data or JSON payloads). In HTTP responses, it shows the format of the data the server sends back to the client.

  6. The Content-Type header helps the recipient (like a web browser or server application) read and process the HTTP message body right. For example, a web browser uses the Content-Type header to decide how to show the content from a server.

In short, the HTTP body content type is very important for making sure clients and servers can share and understand data correctly over the HTTP protocol. It makes sure that both sides can read and handle the payload well, no matter what its format or encoding is.

Binary data transfer

Binary data transfer is the process of sending binary data between systems or over networks. This kind of data can stand for different types of information like images, audio files, video files, programs, and other non-text files.

Base64 is a way to turn data into ASCII characters. Using this method, you can encode links, images, and files into text. It does this by using 64 ASCII characters that include both letters, numbers, and sometimes two extra characters for padding. You often use Base64 encoding when your binary data needs to go through formats or protocols that usually deal with text. For instance:

  • In email attachments, files like pictures or documents change to Base64 text to fit within the email.

  • In HTTP protocols, binary data, like media files, turn into Base64 text to include in JSON or XML payloads.

  • Data URIs use Base64 to turn binary data into a form you can put straight into web pages or style sheets.

HTTP requests or responses might carry binary data, such as multimedia content. In those cases, such as with HTTP or email, you use Base64 encoding to change the binary data into text. So, you may find the payload in Base64 text to make binary data transfer easier over text-based protocols.

Imagine you have an "image.jpg" file, and you need to send it over email, which is text-based. This file is made up of binary data that shows an image.

You change this binary image data to Base64 text, which makes it a string of ASCII characters. Now that it's in text form, you can send it over text-supporting protocols.

Here is a simple example of what a Base64-encoded image might look like:

data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAQAAAAnOwc2AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA
82lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjardA/SgNBHMXx70TULloE67mAgmu15eQPi2AR
YopsqkxmBw1hd4fZn3/2Bh7B43iDFIIXUVJbqARLwU/1eM2DB+rZDPujzjGUlcRsYvJZPteHGw7Y
AwDrmmDG4yuAqq48vynYvqEAXk/NsD/ib/ZdiAK8AEnhGwd8AKsHCQJqAfSW6yCgBOitp5MBqCeg
K/5RAAZ1aOPq5lb0eZqm2hT10uvrthFfNvqycnUMdbTiC+B7A+Aoi7bVmS1Lq5OzhH83y+f6K71P
UYA62ey6HXcX73/+7FzAJ0sbODDOTdGSAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAA
ADqYAAAXb5JfxUYAAACRSURBVHjaBMFdS4NgGADQ89Kjk5z0QXWxDeq26D/2Q+siRjQMa0xNpm3V
OekpCZlcJjn6MTmEzMxc5VSY9FpDyFRuLF3J9WobTShcuHXvTunTM8ZQOLfy4NFcLdlqwomZyrWF
M8mlUh5+Hez1On86g9ExTDq1F0mp8arxHUZba8mHws67jS6MWgzehL3Wl/5/AGY7NAWQ1C5yAAAA
AElFTkSuQmCC

This long list of characters is hiding an image:

Download picture

  • data:image/png; - shows the image is in png format.

  • base64; - tells you the code format that encrypts the image.

  • iVBORw0KGgoAAAANSUhEUgAAAAA... - the actual Base64 code

Copy and paste this code into a new browser tab, and you'll see the picture there.

Today, we use many ways to send binary data, like the well-known HTTP, FTP, TCP/IP, and Websockets. These methods offer different features for data transfer, depending on what the application needs, including how fast it is, how safe, and how it can grow. Also, HTTP can send binary data as it is, without turning it into Base64.

Conclusion

Payloads act like containers that hold many different kinds of data, like user input, configuration settings, API requests and responses, multimedia content, and records from a database. This variety shows how complex the exchange of data is in software development for different platforms and applications.

3 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo