HTTP 415 Unsupported Media Type

Ledz 4 years ago
<?php

$url = "https://demo.traccar.org/api/session";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Accept: application/json",
   "Authorization: Basic bGVkemNvcEBnbWFpbC5jb206cmF0aXJhdGkx",
   "Content-Type: application/json",
   "Content-Length: 0",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

?>

can anyone help me with api login?

Anton Tananaev 4 years ago

Wrong content type, as the error message pretty clearly indicates. This endpoint doesn't accept JSON. You should carefully read the API documentation.

Ledz 4 years ago

Hello Anton yes i finally fixed it, but i have samesite cookie issue. will it work in iframe without ssl? with http in http

Ledz 4 years ago

this is working example, if someone need.

<?php
    $auth = base64_encode("email:password");
    $url = "http://***.***.***.***:8082/api/session";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $headers = array(
       "Accept: application/json",
       "Authorization: Basic $auth",
       "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $data = "email=email&password=password";
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
?>