Jackal
Jackal

Reputation: 3521

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?

Upvotes: 0

Views: 119

Answers (1)

Adrian Edy Pratama
Adrian Edy Pratama

Reputation: 4011

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();
});

Upvotes: 2

Related Questions