Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Eze99's avatar
Level 1

Laravel 8 HTTP Client - How to send a string in a POST request

I need to send a POST request to an API that requires a String as a parameter. I'm using Laravel's HTTP Client to make the requests, but the data format is an array.

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

This is the post() function from 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,
        ]);
    }

The format I get from the request with a 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 => "2g9....wz"
  ]
}

What I need is that the data has the following format:

data: "2g9....wz"

Try changing the content-type to "text/plain", but the string is always kept inside the array.

data: [ "2g9....wz" => 0 ]

Any solution to send only a string inside the data with HTTP Client? Another PHP library that I can use to make POST requests with a parameter of type string? Am I doing something wrong?

0 likes
5 replies
jlrdw's avatar

Can't you include the string in the json? Also you could send another array:

Original

$data['dog'] = a query here;

To add more "data"

$data['dog'] = query here for a listing of dogs;
$data['mystring'] = ['url' => 'your_string_here'];   //Made up data for example
return Response::json($data);

Just example

Eze99's avatar
Level 1

Hello @jlrdw,

Yes, the string is a token that I must generate to get a resource. An example is this:

$string = "NtpcFQj9lQQoWuztFpssFoSQTAwbGReBbl6nc4HKYLEm";

Eze99's avatar
Level 1

What I need is that the value of the $data is just a string because I need to pass the token to it.

Like this:

use Illuminate\Support\Facades\Http;

$response = Http::post($url, "NtpcFQj9lQQoWuztFpssFoSQTAwbGReBbl6nc4HKYLEm");

But I think it is not possible, because HTTP Client uses guzzlehttp/guzzle and in the documentation they explain that only an array can be passed to the body of the request :c

Please or to participate in this conversation.