Send Image Files in an API POST request (2024)

API(Application Programming Interface), it technically refers to a robust set of procedures, tools, and protocols that permit the interaction between web applications. It is an intermediary that delivers a client’s request to the server and then returns a response to the client.

First we will look at what is MIME type❓️A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) is a standard that indicates the nature and format of a document, file, or assortment of bytes. The simplest MIME type consists of a type and a sub-type and An optional parameter can be added to provide additional details:(type/subtype;parameter=value)

There are two classes of type: discrete and multipart. Discrete types are types which represent a single file or medium, such as a single text or music file, or a single video. A multipart type is one which represents a document that’s comprised of multiple component parts.

  • Discrete Types: application, audio, font, image,model, text, video etc
  • Multipart types: Mainly 2 types as message(ex: message/partial),multipart(ex:multipart/form-data)

When we need to send an Image file to an API request there are many options. I will explain some of those methods to send an Image by using the postman.

Option 1: Direct File Upload , From this method you can select form-data and set the type to file. Then select an image file by clicking on the button shown in the value column.

Send Image Files in an API POST request (2)

The content type is automatically detect by postman but if you want you can set it with a relevant MIME type.(image/gif, image/jpg, image/png ..etc.)

Option 2: Send as a Base64 String, In this method you can send the base64 string as a JSON string with the requests. Base64 is a way to encode binary data into an ASCII character format by translating it into a radix-64 representation. I recommend you that never use Base64 for large file/data upload to server because it’s convert whole data and post it to server.

ex:

Send Image Files in an API POST request (3)

Option 3 : By using Multipart

How to send a post request if you have more than one file? How to send a very large file in a post request? The answer is using the multipart content-type.

Let’s look at how we can send image files by using multipart. Multipart is a way to upload file/data to server in the form of part which are in bytes. Multipart/form-data is applied to a form though, so you can send everything in a multi-part form, including “regular” data also.

If you need to send a JSON file containing meta data along with the image file you can use this method. In the case of sending large files you can divide the file into multiple parts and send it as below.

Ex: Here you can see how to send a jpeg file along with a plain text file. The content-type is stated as multipart/form-data and it’s boundary parameter is set to a value ‘Boundary_2_bHash_bTimestamp’. When sending the request this boundary value is added to the start of the full content, end of the full content and end of each part of files included in the request (seen in the request below)

POST /platform/memories/memories
Content-Type: multipart/form-data; boundary=Boundary_2_bHash_bTimestamp
Authorization: Bearer YOUR_ACCESS_TOKEN_HERE
MIME-Version: 1.0
--Boundary_2_bHash_bTimestamp
Content-Type: text/plain
Content-Disposition: form-data; name="title"
Missionary Portrait
--Boundary_2_bHash_bTimestamp
Content-Type: image/jpeg
Content-Disposition: form-data; filename="alma-mission.jpg"; name="artifact"
...(binary bytes of the image)...
--Boundary_2_bHash_bTimestamp--

How we can do this with postman? You just need to add your files to the form-data. Thank to postman we don’t need to set content types, the content-types and boundary is automatically detect by postman.

ex:

Send Image Files in an API POST request (4)

Base64 image vs Multipart request:

Base64 image

  • Base64 converts your data to an ASCII representation of the binary data.
  • Base64 increases the size of the data transferred by 33%.
  • Base64 image representation can be directly placed within html to render an image.

Multipart request

  • Multipart/form-data is the standard way of transferring binary data in HTTP requests.It allows you to use specific encoding / content types for each part you’d like to transfer.
  • Binary takes up less space. And benefits from greater network effects and standardization. E.g. if you want to use amazon simple secure storage S3 you have to store a binary file. You can’t store a string you would need a key/value store e.g. redis.

It’s easier to send these requests by using postman. Also you can send these requests by using the Curl. I thank you all for reading this article and if you have anything to improve on it please leave me a comment.😊️

Send Image Files in an API POST request (2024)

FAQs

Can you send an image in a post request? ›

The content type is automatically detect by postman but if you want you can set it with a relevant MIME type. (image/gif, image/jpg, image/png ..etc.) Option 2: Send as a Base64 String, In this method you can send the base64 string as a JSON string with the requests.

Can I send images in the rest API? ›

How to send image files in REST API. If your file upload API issues relate to images, there is a simple solution – direct file uploads. You can use this to solve the problem of how to send an image file in a REST API.

How to upload an image using post API? ›

You should upload the file and get the url to the file, then post it. This worked for me: r = requests. post(f'{rooturl}/uploads. json', files = {'files[]': (file, open(file, 'rb'), 'image/png')}, data={'type':'image'}, headers={ "Api-Username" : sys.

Can you send a file in a POST request? ›

You can send files and binary data directly to Media Server using a POST request. One possible way to send a POST request over a socket to Media Server is using the cURL command-line tool. The data that you send in a POST request must adhere to specific formatting requirements.

How to send an image in a JSON request? ›

The best you could do with JSON is encode all the raw image data using base 64. That would be making use of 6 bits per byte instead of 8. If you have a MongoDb, you could store the image as a document. If you have an SQL database, it is best to store raw image data using the BLOB type.

Can you send files through REST API? ›

Use the File transfer REST API to upload and download files using HTTP or HTTPS as the transport protocol and to list the contents of a directory.

How to send an image in HTTP response? ›

Sending HTTP responses manually

// create a new image resource $img = Image::canvas(800, 600, '#ff0000'); // send HTTP header and output image data header('Content-Type: image/png'); echo $img->encode('png'); Read more about HTTP responses in the api documentation.

Can Postman display images? ›

Postman is a powerful tool for testing APIs, but when it comes to displaying images, it can be a little bit tricky. This article, the process of setting up an Express server to serve images and accessing them using Postman.

What can you send in a POST request? ›

As part of a POST request, an arbitrary amount of data of any type can be sent to the server in the body of the request message. A fields header field in the POST request usually indicates the message body's Internet media type.

Are images sent in message requests covered? ›

Keep in mind that any photos or videos sent to you in a message request will be hidden by a dark image. If you tap on the dark image, a blurred photo will be displayed. If you tap on the blurred photo or directly reply to the message request, the photo or video will be displayed.

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 6039

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.