Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ncltours's avatar

How we capture the request body into our controller when someone uses this api?

https://somesite.com/getTicketing

The above is api url which I have created.

When someone uses my api, He/She sends sends post data in following format

{ "airPnr": '4454554', "orderNum": "1024758120", "ticketNums": [ { "psgName": "name", "ticketNum": "9990000000000" } ] }

And I send response with success/false

The question is How do I capture the request sent by users.

My code is as follows:

public function getTicketing(Request $request) {

    $orderNum             = $request->input('orderNum');
    $airPnr               = $request->input('airPnr');
    $ticketNum            = $request->input("ticketNum");
    $psgName              = $request->input("psgName");

    if(!empty($orderNum))
      {

        $ticketing                = new Ticketing;
        $ticketing->orderNum      = $orderNum;
        $ticketing->airPnr        = $airPnr;
        $ticketing->ticketNum     = $ticketNum;
        $ticketing->psgName       = $psgName; 
        $ticketing->save();     

        return response()->json(['errorCode' => 0, 'errorMsg' => 'ok']);
    }

    else
    
    {
        return response()->json(['errorCode' => 1, 'errorMsg' => 'Failure reason']);
    }   

}

I just want to capture the $orderNum from the above request which the users send me?

0 likes
3 replies
ftiersch's avatar

Your approach is right. Possibly though laravel doesn't see, that the request is JSON encoded. If they send you json data they should send along a Content-Type header with application/json so Laravel can understand what kind of data it is

ncltours's avatar

Hi Thanks for response. It was helpful.

martinbean's avatar
Level 80

@ncltours If the client is sending the data as JSON, then you can get it like this:

$data = json_decode($request->getContent());

But your API endpoint should be tightly defined as to what format it accepts data in, as you’ll then be able to parse it easier if you know what you’re working with.

Please or to participate in this conversation.