Home > Enterprise >  Tried to made an POST request using Guzzle
Tried to made an POST request using Guzzle

Time:06-23

Im so new using API in my project, so i tried to make an POST request using Guzzle in Laravel, but i really dont know to do it, i've been seraching on the internet how to but i can't find the answer, here's what i've been tried:

    $headers = [
        'Content-Type' => 'application/json',
        'signature' => '73ceef837b9be3cf098eca4a4697bd6a36718b64b0cf407c4324415941ff9780',
        'va' => '0000002298436631',
        'timestamp', '20191209155701'
    ];
    $body = '{
        "name": "Dudy",
        "phone": "082298436631",
        "email": "[email protected]",
        "amount": "10000",
        "notifyUrl": "https://mywebsite.com",
        "expired": "24",
        "expiredType": "hours",
        "comments": "Catatan",
        "referenceId": "1",
        "paymentMethod": "qris",
        "paymentChannel": "qris"
    }';
    $request = new Request('POST', 'https://sandbox.ipaymu.com/api/v2/payment/direct', 
   $headers, $body);

And i tried to do it on postman and its work perfectly, here's the setting on my postman:

enter image description here enter image description here

CodePudding user response:

You can use Laravel's Http Facade to deal with requests (easier to test later, if needed):

$headers = [];
$body = [];

$request = Http::send(
    method: 'POST',
    url: 'https://your.url/',
    options: [
        'headers' => $headers,
        'form_params' => $body,
    ]
);

CodePudding user response:

$response = Http::withHeaders([
    'X-First' => 'foo',
    'X-Second' => 'bar'
])->post('http://example.com/users', [
    'name' => 'Taylor',
]);

I show a example code on top. I provide your code below.

    $response = Http::withHeaders ([
            'Content-Type' => 'application/json',
            'signature' => '73ceef837b9be3cf098eca4a4697bd6a36718b64b0cf407c4324415941ff9780',
            'va' => '0000002298436631',
            'timestamp', '20191209155701'
        ])->post('https://sandbox.ipaymu.com/api/v2/payment/direct',[
            "name": "Dudy",
            "phone": "082298436631",
            "email": "[email protected]",
            "amount": "10000",
            "notifyUrl": "https://mywebsite.com",
            "expired": "24",
            "expiredType": "hours",
            "comments": "Catatan",
            "referenceId": "1",
            "paymentMethod": "qris",
            "paymentChannel": "qris"
]);
   

CodePudding user response:

Try this

$client = new \GuzzleHttp\Client();

$response = $client->post(
    'https://sandbox.ipaymu.com/api/v2/payment/direct',
    [
         'form_params' => [
             "name" => "Dudy",
             "phone" => "082298436631",
             "email" => "[email protected]",
             "amount" => "10000",
             "notifyUrl" => "https://mywebsite.com",
             "expired" => "24",
             "expiredType" => "hours",
             "comments" => "Catatan",
             "referenceId" => "1",
             "paymentMethod" => "qris",
             "paymentChannel" => "qris"
          ]
      ],
      [
          'headers' => [
              'Content-Type' => 'application/json',
              'signature' => '73ceef837b9be3cf098eca4a4697bd6a36718b64b0cf407c4324415941ff9780',
              'va' => '0000002298436631',
              'timestamp', '20191209155701'
          ]
      ]
);

dd($response) //response data
  • Related