0

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

Mohammad Edris Raufi
  • 1,393
  • 1
  • 13
  • 34
  • Why would you pass data of that length as a URL parameter to begin with? You're probably in danger of hitting the limit of URL size that common clients allow for. (And you did not even apply proper URL encoding.) – CBroe Oct 08 '21 at 07:53
  • 2
    Also I'm wondering, whether that error message is actually supposed to mean that the JSON was _invalid_ - or whether it might just be telling you, that what data you are sending is _structured_ wrong. – CBroe Oct 08 '21 at 07:54
  • What have you tried to resolve the problem? Where are you stuck? Have you checked whether Postman does anything different than Guzzle? – Nico Haase Oct 08 '21 at 08:06
  • ....for example: why does your request in Postman start with a `data` key, while your Guzzle request does not? – Nico Haase Oct 08 '21 at 08:07
  • Thank you, everyone, for mentioning my mistake Now I send data via form but still gets the same error – Mohammad Edris Raufi Oct 08 '21 at 08:17
  • And what have you tried to resolve that problem? Why not debug the request that is sent? As far as I see, you still don't send everything wrapped in a `data` element – Nico Haase Oct 08 '21 at 08:49
  • Thanks, I edited the question. even with this still same problem – Mohammad Edris Raufi Oct 08 '21 at 08:54
  • And what have you tried to resolve the problem? Where are your debugging attempts? – Nico Haase Oct 08 '21 at 09:16

1 Answers1

0

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

rfso
  • 41
  • 1
  • 7
  • As you said I did everything but still not working I try it on https://reqbin.com/req/javascript/uzf79ewc/javascript-post-request-example it works fine with my data but in laravel, it doess not. – Mohammad Edris Raufi Oct 08 '21 at 08:30
  • By the way https://reqbin.com/req/javascript/uzf79ewc/javascript-post-request-example is very useful up vote for the link – Mohammad Edris Raufi Oct 08 '21 at 09:12