0

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?

1 Answer 1

2

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();
});
Sign up to request clarification or add additional context in comments.

4 Comments

doesn't work, i need to send the postal code as parameter to the api. I think i need to pass the value in the route, access to it inside this function and send on the api call
I can access the variable now, your example will be correct if you edit it to send this variable to the API in the parameters. Tested and works
edited your question so others can see the working example
Reminder, if you need more than one variable later, the parameter for the function will follow the route parameter order, naming isn't important except you are using binding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.