skip to Main Content

I am trying to send an API request to the endpoint, and it is successful via Postman raw JSON, Now I am implementing the same in Laravel with HTTP facade.

Below is a POSTMAN screenshot with the successful response from the endpoint.

enter image description here

Now in Laravel I am struggling to send request, I tried below code, but yes, it shows unexpected {

        $response = Http::withBody(jData={"apkversion": "1.0.0",
            "uid": "dsfee3",
            "pwd": "deeddee",
            "factor2": "680208",
            "vc":"eefsdf",
            "appkey":"wefdsesdfd334",
            "imei": "abc1234",
            "source": "API"
            })->post('https://api.shoonya.com/NorenWClientTP/QuickAuth');

How can I send a request with given parameters as raw JSON?

2

Answers


  1. Chosen as BEST ANSWER

    It was resolved by using below code...

     $response = Http::withBody("jData=".json_encode([
            "apkversion" => "1.0.0",
            "uid" => "dsfee3",
            "pwd" => "deeddee",
            "factor2" => "680208",
            "vc" => "eefsdf",
            "appkey" => "wefdsesdfd334",
            "imei" => "abc1234",
            "source" =>  "API"
        ])->post('https://api.shoonya.com/NorenWClientTP/QuickAuth');
    
    

  2. When using the Http::withBody() you need to provide the body as a string and specify the content type. However, for sending JSON data, it’s more common to use the Http::post() with ->body() or ->json() to automatically handle the JSON encoding and set the appropriate headers.

    use IlluminateSupportFacadesHttp;
    
    $response = Http::withHeaders([
          'Content-Type' => 'application/json',
        ])
        ->post('https://api.shoonya.com/NorenWClientTP/QuickAuth', [
           "jData" => [
              "apkversion" => "1.0.0",
              "uid" => "dsfee3",
              "pwd" => "deeddee",
              "factor2" => "680208",
              "vc" => "eefsdf",
              "appkey" => "wefdsesdfd334",
              "imei" => "abc1234",
              "source" => "API",
           ]
       ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search