Stuck with Laravel PHP, need assistance

Hello everyone,

I am trying to access the API via my laravel application but i keep getting an error. Can someone please help me?

This is my code:

$payload = '{

            "vehicles": [

            {

            "vehicle_id": "vehicle-1",

            "type_id": "cargo-bike",

            "start_address": {

            "location_id": "berlin",

            "lon": 13.406,

            "lat": 52.537

            },

            "earliest_start": 1554804329,

            "latest_end": 1554808329,

            "max_jobs": 3

            },

            {}

            ],

            "vehicle_types": [

            {

            "type_id": "cargo-bike",

            "capacity": [

            10

            ],

            "profile": "bike"

            }

            ],

            "services": [

            {

            "id": "s-1",

            "name": "visit-Joe",

            "address": {

            "location_id": "13.375854_52.537338",

            "lon": 13.375854,

            "lat": 52.537338

            },

            "size": [],

            "time_windows": []

            },

            {

            "id": "s-2",

            "name": "serve-Peter",

            "address": {

            "location_id": "13.393364_52.525851",

            "lon": 13.393364,

            "lat": 52.525851

            },

            "size": []

            },

            {

            "id": "s-3",

            "name": "visit-Michael",

            "address": {

            "location_id": "13.416882_52.523543",

            "lon": 13.416882,

            "lat": 52.523543

            },

            "size": [

            1

            ]

            },

            {

            "id": "s-4",

            "name": "do nothing",

            "address": {},

            "size": []

            }

            ],

            "shipments": [

            {}

            ],

            "objectives": [

            {},

            {}

            ],

            "configuration": {

            "routing": {

            "calc_points": true,

            "snap_preventions": []

            }

            }

            }';

        $client = new \GuzzleHttp\Client();

  
        $response = $client->request('POST', 'https://graphhopper.com/api/1/vrp?key=a79dmyapikeyd3b29', [

            'headers' => [

                //'User-Agent' => 'testing/1.0',

                'Accept-Encoding' => 'gzip',

                'Accept'     => 'application/json',

                //'X-Foo'      => ['Bar', 'Baz']

            ],

            'decode_content' => false,

            'body'  => json_encode($payload),

        ]);

And this is the error i get:

[2022-05-09 18:11:47] local.ERROR: Client error: `POST https://graphhopper.com/api/1/vrp?key=a79de7d9-ffe8-461e-a20b-95c03cdd3b29` resulted in a `400 Bad Request` response:

{"message":"Unsupported content type ","status":"finished"}

 {"userId":1,"exception":"[object] (GuzzleHttp\\Exception\\ClientException(code: 400): Client error: `POST https://graphhopper.com/api/1/vrp?key=a79de7d9-ffe8-461e-a20b-95c03cdd3b29` resulted in a `400 Bad Request` response:

{\"message\":\"Unsupported content type \",\"status\":\"finished\"}

 at D:\\WebServer\\ZeinTekWebServices\\liveroot\\merlin-core\\vendor\\guzzlehttp\\guzzle\\src\\Exception\\RequestException.php:113)

Did you try to set the "Content-Type: application/json" header?

1 Like

will try that right now, thanks!!

Your answer was super helpful, the error message changed to what seemed an encoding issue.

I then removed the json encoding i had added and then it worked. I now have a 400 error message but it’s because my free plan only allows for one vehicule.

Do you have graphhopper implemented in your own project? I would love to get in touch with you.

Thanks a million!!!

Leaving this here for anyone who might find it useful:


		$client = new \GuzzleHttp\Client();
		
		$response = $client->request('POST', 'https://graphhopper.com/api/1/vrp?key=a79de7d9-ffe8-461e-a20b-95c03cdd3b29', [
			'headers' => [
				'Accept-Encoding' => 'gzip',
				//'Accept'     => 'application/json',
				'Content-Type' => 'application/json',
			],
			'decode_content' => false,
			//'body'	=> json_encode($payload),
			'body'	=> $payload,
		]);
		
1 Like

Do you have graphhopper implemented in your own project? I would love to get in touch with you.

I’m part of the GraphHopper team. Feel free to contact me via mail, PM or the forum if you have questions.

1 Like

Oh very nice. What is your email and how do I address you?

BTW, how do we mark this as solved?

Just use the direct message feature of the forum

We don’t really close forum discussions, or at least I don’t know how this works either.

Powered by Discourse