返回

如何在POST请求中发送字符串

发布时间:2022-06-02 23:45:15 464
# json

我需要向需要字符串作为参数的API发送POST请求。我正在使用Laravel的HTTP客户端发出请求,但是data格式为数组。

$response = Http::acceptJson()->withHeaders([
                'Connection' => 'keep-alive',
                'Content-Type' => 'application/json'
            ])->post($url, [ "NtpcFQj9lQQoWuztFpssFoSQTAwbGReBbl6nc4HKYLEm" ]);

这是post()功能来自Illuminate\Http\Client\PendingRequest

/**
     * Issue a POST request to the given URL.
     *
     * @param  string  $url
     * @param  array  $data
     * @return \Illuminate\Http\Client\Response
     */
    public function post(string $url, array $data = [])
    {
        return $this->send('POST', $url, [
            $this->bodyFormat => $data,
        ]);
    }

我从带有Http::dd()

^ Illuminate\Http\Client\Request {#1381 ▼
  #request: GuzzleHttp\Psr7\Request {#1378 ▼
    -method: "POST"
    -requestTarget: null
    -uri: GuzzleHttp\Psr7\Uri {#1366 ▶}
    -headers: array:6 [▼
      "Content-Length" => array:1 [▶]
      "User-Agent" => array:1 [▶]
      "Host" => array:1 [▶]
      "Accept" => array:1 [▼
        0 => "application/json"
      ]
      "Connection" => array:1 [▼
        0 => "keep-alive"
      ]
      "Content-Type" => array:1 [▼
        0 => "application/json"
      ]
    ]
    -headerNames: array:6 [▶]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#1369 ▶}
  }
  #data: array:1 [▼
    0 => "NtpcFQj9lQQoWuztFpssFoSQTAwbGReBbl6nc4HKYLEm"
  ]
}

我需要的是data具有以下格式:

data: "NtpcFQj9lQQoWuztFpssFoSQTAwbGReBbl6nc4HKYLEm"

尝试将内容类型更改为“text/plain”,但字符串始终保留在数组中。

data任何解决方案只在HTTP 客户端内发送一个字符串?我可以使用另一个 PHP 库来使用字符串类型的参数发出 POST 请求?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像