elfeffe's avatar

POST with one extra parameter

I need to pass a JSON and 1 extra parameter to my API.

I have this route $app->post('api/v1/event','EventController@saveEvent'); But I need to pass one extra parameter like: $app->post('api/v1/event/{api}','EventController@saveEvent');

I need to get the value in "api" and then the JSON. Is that possible?

0 likes
6 replies
pmall's avatar

Isn't this just a controller action parameter? Can you elaborate a bit?

Snapey's avatar

It just becomes part of the URI for the post? (as you have shown) and will be passed to the saveEvent in the method call.

willvincent's avatar

Sure, change your route to this:

$app->post('api/v1/event/{api?}','EventController@saveEvent');

Then the controller...

public function saveEvent(Request $request, $api = NULL) {
  // If included in the request url, $api will be set to that value, else null by default.
  // json is contained within $request->input
}
1 like
elfeffe's avatar

It works when I access using

/api/v1/event/1234

I get 1234 in $api and the JSON on $request->getContent()

It doesn't work if I send /api/v1/event with an empty api, is possible to use it without api?

willvincent's avatar
Level 54

Maybe optional params aren't allowed for post routes? Try this...

routes:

$app->post('api/v1/event','EventController@saveEvent');
$app->post('api/v1/event/{api}','EventController@saveEvent');

controller:

public function saveEvent(Request $request, $api = NULL) {
  // If included in the request url, $api will be set to that value, else null by default.
  // json is contained within $request->input
}
1 like
elfeffe's avatar

It works, thank you very much!

1 like

Please or to participate in this conversation.