Skip to content

Compress File

In this example, an imported jpg file is compressed. The import task ID must be provided as the input so that FreeConvert uses the correct imported file for the compression.

Depending on the input file format, different compression options are available. These are part of the advanced options that allow you to control the output file. The available options for a specific input and output combination, including hints, can be retrieved from the advanced options endpoint.

curl -X POST https://api.freeconvert.com/v1/process/compress \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer {access_token}'

# Body Parameter
{
  "input": "import_task_id",
  "input_format": "jpg",
  "options": {
    # …advanced options…
  },
}
POST https://api.freeconvert.com/v1/process/compress HTTP/1.1
Host: api.freeconvert.com
Content-Type: application/json
Accept: application/json

# Body Parameter
{
  "input": "import_task_id",
  "input_format": "jpg",
  "options": {
    <!-- …advanced options -->
  },
}
const inputBody = {
  input: import_task_id,
  input_format: "jpg",
  options: {
    // …advanced options…
  },
};
const headers = {
  "Content-Type": "application/json",
  Accept: "application/json",
  Authorization: `Bearer ${access_token}`,
};

fetch("https://api.freeconvert.com/v1/process/compress", {
  method: "POST",
  body: inputBody,
  headers: headers,
})
  .then(function (res) {
    return res.json();
  })
  .then(function (body) {
    console.log(body);
  });
import requests

request_body = {
  'input': import_task_id,
  'input_format': 'jpg',
  'options': {
    # …advanced options…
  }
}
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer' + access_token
}

result = requests.post(
  'https://api.freeconvert.com/v1/process/compress',
  data = request_body,
  headers = headers
)

print(result.json())
require 'rest-client'
require 'json'

request_body = {
  'input' => import_task_id,
  'input_format' => 'jpg',
  'options': {
    # …advanced options…
  }
}
headers = {
  'Content-Type' => 'application/json',
  'Accept' => 'application/json',
  'Authorization' => 'Bearer' + access_token
}

result = RestClient.post(
  'https://api.freeconvert.com/v1/process/compress',
  body: request_body,
  headers: headers
)

JSON.parse(result)
<?php

require 'vendor/autoload.php';

$headers = array(
  'Content-Type' => 'application/json',
  'Accept' => 'application/json',
  'Authorization' => 'Bearer ' $access_token,
);

$client = new \GuzzleHttp\Client();

$request_body = array(
  'input' => import_task_id,
  'input_format' => 'jpg',
  'options' => {
    // …advanced options…
  },
)

try {
  $response = $client->request(
    'POST',
    'https://api.freeconvert.com/v1/process/compress',
    array(
      'headers' => $headers,
      'content' => json_encode($request_body),
    )
  );
  print_r($response->getBody()->getContents());
}
catch (\GuzzleHttp\Exception\BadResponseException $e) {
  // handle exception or api errors.
  print_r($e->getMessage());
}

// ...
URL obj = new URL("https://api.freeconvert.com/v1/process/compress");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(
  new InputStreamReader(con.getInputStream())
);

String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
  response.append(inputLine);
}
in.close();

System.out.println(response.toString());
package main

import (
  "bytes"
  "net/http"
)

func main() {
  jsonReq := map[string][]string{
    "input": string{import_task_id},
    "input_format": string{"jpg"},
    "options": map[string[]]string{
      // …advanced options…
    }
  }
  headers := map[string][]string{
    "Content-Type": string{"application/json"},
    "Accept": string{"application/json"},
    "Authorization": string{"Bearer" + access_token},
  }

  data := bytes.NewBuffer([]byte{jsonReq})

  req, err := http.NewRequest(
    "POST",
    "https://api.freeconvert.com/v1/process/compress",
    data
  )
  req.Header = headers

  client := &http.Client{}
  resp, err := client.Do(req)
  // ...
}