HTTP Requests - Image Upload

HTTP Requests / Image Upload

On this page:
POST https://api.optidash.ai/1.0/upload
FieldDescription
fileBinary representation of the image to be processed.
dataSerialized JSON object with processing parameters.

To upload an image for processing, send a POST multipart request to https://api.optidash.ai/1.0/upload endpoint which must consist of two fields: file and data. Within the file field you must transmit binary data (an image for processing) whereas the data field is reserved for a serialized JSON object with image processing steps.

An example cURL request would look like the following:

curl https://api.optidash.ai/1.0/upload -X POST -u your-api-key: \
-F file=@/path/to/input.jpg \
-F data='{
    "resize": {
        "width": 100,
        "height": 75
    }
}'

Code Examples for Image Upload

Below, you'll find examples of how to use the Image Upload method from the Optidash API:

  • PHP
  • Node
  • Ruby
  • Go
  • Python
  • Java
<?php

/**
* Instantiate new `$opti` by calling a constructor
*/

$opti = new Optidash\Optidash('your-api-key');


/**
* Upload an image from disk, resize it to 100 x 75
* and parse the output metadata
*/

$opti
    ->upload('/path/to/input.jpg')
    ->resize(array(
        'width' => 100,
        'height' => 75
    ))
    ->toJSON(function ($error, $meta) {
        if (!empty($error)) {
            throw new Exception($error);
        }

        /**
        * You'll find the full JSON metadata array within the `$meta` variable.
        * Remember to always check if the `success` property is set to `true`.
        */

        if ($meta['success'] == true) {
            print ($meta['output']['url']);
        } else {
            print ($meta['message']);
        }
    });
/**
* Module dependencies
*/

const Optidash = require("optidash");


/**
* Instantiate new `opti` by calling a constructor
*/

const opti = new Optidash("your-api-key");


/**
* Upload an image from disk, resize it to 100 x 75
* and parse the output metadata
*
* Optidash Node module enables you to provide a file path
* or a readable stream as an argument for .upload() method
*/

opti
    .upload("path/to/input.jpg")
    .resize({
        width: 100,
        height: 75
    })
    .toJSON((err, meta) => {
        if (err) {
            return console.log(err);
        }

        /**
        * You'll find the full JSON metadata hash within the `meta` variable.
        * Remember to always check if the `success` property is set to `true`.
        */

        if (meta.success) {
            console.log(meta.output.url);
        } else {
            console.log(meta.message);
        }
    });
##
# Require dependencies

require "optidash"


##
# Instantiate new `opti` by calling a constructor

opti = Optidash.new("your-api-key")


##
# Upload an image from disk, resize it to 100 x 75
# and parse the output metadata

err, meta = opti
    .upload("path/to/input.jpg")
    .resize(
        width: 100,
        height: 75
    )
    .to_json

abort(err) if err


##
# You'll find the full JSON metadata hash within the `meta` variable.
# Remember to always check if the `success` property is set to `true`.

if meta["success"]
    puts meta["output"]["url"]
else
    puts meta["message"]
end
package main

import (
    "fmt"
    "github.com/optidash/optidash-go"
)

func main() {

    /**
    * Instantiate new `opti` by calling a constructor
    */

    opti, err := optidash.NewClient("your-api-key")

    if err != nil {
        panic(err)
    }


    /**
    * Upload an image from disk, resize it to 100 x 75
    * and parse the output metadata
    *
    * Optidash Golang package enables you to provide a file path
    * or an io.Reader as an argument for .Upload() method
    */

    meta, err := opti.
        Upload("path/to/input.jpg").
        Resize(optidash.P{
            "width": 100,
            "height": 75
        }).
        ToJSON()

    if err != nil {
        panic(err)
    }


    /**
    * You'll find the full JSON metadata hash within the `meta` variable.
    * Remember to always check if the `success` property is set to `true`.
    */

    if meta["success"].(map[string]interface{})["success"].(bool) {
        fmt.Println(meta["output"].(map[string]interface{})["url"])
    } else {
        fmt.Println(meta["message"])
    }
}
##
# Import dependencies

from optidash import optidash


##
# Instantiate new `opti` by calling a constructor

opti = optidash('your-api-key')


##
# Upload an image from disk, resize it to 100 x 75
# and parse the output metadata
#
# Optidash Python package enables you to provide file path
# or BytesIO/cStringIO object as an argument for .upload() method

err, meta = (
    opti
        .upload('path/to/input.jpg')
        .resize({
            'width': 100,
            'height': 75
        })
        .toJSON()
)

if err is not None:
    raise StandardError(err)


##
# You'll find the full JSON metadata hash within the `meta` variable.
# Remember to always check if the `success` property is set to `true`.

if meta['success'] is True:
    print (meta['output']['url'])
else:
    print (meta['message'])
package com.optidash.examples;

/**
* Import dependencies
*/

import static com.optidash.OperationConfiguration.settings;
import com.optidash.Optidash;
import com.optidash.Upload;
import com.optidash.Response;

public class ImageUpload {
    public static void main(String[] args) {

        /**
        * Instantiate new `opti` by calling a constructor
        */

        Optidash opti = new Optidash("your-api-key");

        /**
        * Upload an image from disk, resize it to 100 x 75
        * and parse the output metadata
        */

        final Upload upload = opti.upload("path/to/input.jpg");

        upload.resize(
            settings()
                .set("width", 100)
                .set("height", 75)
        );

        final Response response = upload.toJson();

        /**
        * You'll find the full JSON metadata hash within the `Response#getMetadata()` property.
        * Remember to always check if the `Response#successful` property is set to `true`.
        */

        if (response.isSuccessful()) {
            System.out.println(response.getMetadata().getOutput().get("url"));
        } else {
            System.out.println(response.getMessage());
        }
    }
}
Grace Przemek Magda Damian
Have questions? Let's talk!We usually respond in just under an hour

Thanks for reaching out!

We've received your message
and will get to it as quickly as possible.

Whoops, looks like something went wrong.

We encountered an unexpected error and cannot complete your request. Please try reloading the page.