4
votes

I've been trying for a while to send an image via Postman to a simple PHP script that should display the sent image. The steps I followed were:

Postman "client" side:

  1. Select POST request
  2. Select HEADER Content-Type with value multipart/form-data
  3. In the Body tab select form-data introduce the key = image_test, change from Text to File and search for a image, ex: photo.png
  4. Send POST request.

In the PHP script I just have: echo '<img src="data:multipart/form-data,' . $_POST['image_test'] .'"/>';

This solution isn't working for me although with print_r($_POST) I'm able to see the encoded image. Could this be a problem of the host (https://ide.c9.io) or Postman? I made some other tests with Postman and base64 images and the Display tab of Postman wasn't able to display the decoded images; the only way was to access through the browser.

2
I can understand that you are receiving the image on the server with print_r($_POST)?, so I guess postman is doing its job? What is that you see when using the print_r?Pablo Palacios

2 Answers

1
votes

Some code would help to understand what you are trying to do. You could try to send the image back to the client in base64 encoding. See How to convert an image to base64 encoding? for an example

0
votes

That link was useful to find the solution to this simple example. Basically the process to send data (in this case a image) from Postman is as follows:

  1. Select POST request.
  2. In body select form-data change type to file and enter a key for it.

The server will properly process it with a example like this one:

<!DOCTYPE html>
<html>
    <head>
        <title>Receiving data with Postman</title>
    </head>

    <?php 
        $dir = '/';
        $file = basename($_FILES['image']['name']);

        if (move_uploaded_file($_FILES['image']['tmp_name'], $file)) {
            echo "Ok.\n";

        } else {
            echo "Error.\n";
        }
    ?>
    <img src="<?=$file?>"></img>

</html>