Skip to content
Advertisement

Laravel query string into api call in routes

I have this route

Route::get('/getLocation/{postalCode}', function () {
    $response = Http::get('https://theapi');
    return $response->json();
});

Sadly this doesn’t work

I can reach the api with

https://theapi?PostalCode=xxxx

How can i replicate this in my route to work?

Advertisement

Answer

You got double }} there, try delete it so it will be like this:

Route::get('/getLocation/{postalCode}', function () {
    $response = Http::get('https://theapi');
    return $response->json();
});

Edit:

Add the route parameter to API function parameters

Route::get('/getLocation/{postalCode}', function ($postalCode) {
    // do whatever you want to postalCode variable
    $response = Http::get('https://theapi', [
        'PostalCode' => $postalCode
    ]);
    return $response->json();
});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement