【问题标题】:How to send body request as string in Laravel 7?如何在 Laravel 7 中将正文请求作为字符串发送?
【发布时间】:2020-09-07 00:03:26
【问题描述】:

我的情况是

  • 使用 Laravel 7
  • 想使用 Shopee API

在 Laravel 中发送请求应该是这样的

$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
    ]);

但是 Shopee API 在正文部分不需要空格(不能以 JSON 格式发送)。我试过了

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

它不起作用,因为它必须是一个数组。返回错误Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given .

【问题讨论】:

  • 你能解释一下But Shopee API needs no space in the body part

标签: laravel request laravel-7


【解决方案1】:

试试这个:

$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);

或者:

$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2021-07-07
    • 1970-01-01
    • 2019-10-27
    • 2016-03-28
    • 1970-01-01
    相关资源
    最近更新 更多