0

My situation is

  • using Laravel 7
  • want to use Shopee API

Sending a request in Laravel should be like this

$res = Http::withHeaders([
        'Content-Type' => 'application/json',
        'Authorization' => $secret_key
    ])->post($api_url, [
        "ordersn_list" => [$order_no],
        "shopid" => $shop_id,
        "partner_id" => $partner_id,
        "timestamp" => $timestamp
    ]);

But Shopee API needs no space in the body part (cannot send as JSON format). I have tried

$res = Http::withHeaders([
        'Content-Type' => 'application/json',
        'Authorization' => $secret_key
    ])->post($api_url, $body_string);

It does not work because it must be an array. return error Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given .

Kmp
  • 15
  • 6

1 Answers1

0

Try this:

$data = [
    "ordersn_list" => [$order_no],
    "shopid" => $shop_id,
    "partner_id" => $partner_id,
    "timestamp" => $timestamp
];

$res = Http
    ::asJson()
    ->withHeaders([
        'Authorization' => $secret_key
    ])
    ->post($api_url, $data);

Or:

$data = [
    "ordersn_list" => [$order_no],
    "shopid" => $shop_id,
    "partner_id" => $partner_id,
    "timestamp" => $timestamp
];

$res = Http
    ::withHeaders([
        'Authorization' => $secret_key
    ])
    ->withBody(json_encode($data), 'application/json')
    ->post($api_url);
IndianCoding
  • 2,602
  • 1
  • 6
  • 12