'Json Cannot be Parsed

I am trying to post an object using Guzzle laravel which I pass it using form submit from my view but when I do it says INVALID FORMAT Json Cannot Be Parsed

My View

            <form method="POST" action="comparePrices">
                @csrf
                <input type="text" name="datas" id="textsss"/>
                <input type="submit" value="save"/>
            </form>

But when I post it via postman it works fine

My Controller

        public function comparePrices(Request $request)
    {
        $token = DB::table('a_p_is_tokens')->select('*')->limit(1)->orderBy('id', 'desc')->get()->pluck('token')[0];
        $client = new Client();

        try {
            $res = $client->post('https://test.api.amadeus.com/v1/shopping/flight-offers/pricing', [

                'headers' => [
                    'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token,
                ],
                'form_params' => [
                    "data"=>[
                        "type" => "flight-offers-pricing",
                        "flightOffers" => $request->datas,
                    ],
                // 'body'    => array('data'=>$items),
            ]);

            $res = json_decode($res->getBody()->getContents(), true);
            $response = $res->getResponse();

            print_r(json_decode($response->getBody(), true));
            // return view('agents.agentsTickets');
        } catch (RequestException $e) {
            $response = $e->getResponse();
            $result =  json_decode($response->getBody()->getContents());
            return response()->json(['data' => $result]);
            // return [json_decode($request->datas)];

        }
        dd([$items]);
        return view('agents.agentsTickets');
    }

Error Code

{
  "data": {
    "errors": [
      {
        "code": 477,
        "title": "INVALID FORMAT",
        "detail": "JSON cannot be parsed",
        "status": 400
      }
    ]
  }
}

enter image description here



Solution 1:[1]

Your js code does http GET not http POST

window.location='postings/'+items;

If it should be possible to add "items" into url abd perform http GET then you could try to do the same in Postman - use GET not POST and modify the url, not the body.

If you intend to do in js the same as in Postman then I suggest to see answers from pass post data with window.location.href and modify your js code. Or try https://reqbin.com/req/javascript/uzf79ewc/javascript-post-request-example

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1