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

isarantoglou's avatar

Send XML body with HTTP Client (Laravel 7)

Hello,

I am trying to learn HTTP Client of Laravel 7 and I got stuck. I want to send a XML to an endpoint but I can't find any documentation (https://laravel.com/docs/7.x/http-client#introduction). As I understand the default behavior is JSON.

Here is my code:

$headers = [ .. ];

$xml = ... // Valid XML created with DOMDocument


Http::withHeaders($headers)->post('https://test.com/',
             $xml
        );

Also the response is XML.

Is this doable with the Http client or shall I use guzzle?

Thanks in advance, Ilias

0 likes
14 replies
ftiersch's avatar

JSON and XML are both simply text. What the other side makes of the text depends on the header you send with it.

Just use the header "Content-Type" with "application/xml" (I think) and if the endpoint can handle XML it should be okay.

1 like
isarantoglou's avatar

I thought so too, but it seems the endpoint send back 400 - Bad request. It appears that Http client wraps the body in double quotes:

"<?xml version=\"1.0\" ....."

That is how the raw output starts. Not sure if this is supposed to be like this or not.

Trying to verify the received XML (made my own endpoint and tested it) with DOMDocument I get the following:

Start tag expected, '<' not found

While if I remove the double quotes it is valid.

ryanchantc's avatar

This is how i use Laravel Http component to send xml in POST request

$response = Http::send('POST', $this->url, [
            'body' => $xml,
]);
1 like
skeith22's avatar

@ryanchantc This doesn't work at all.

@isarantoglou I have the same problem and I wonder why it doesn't work

This one works though, tested on a real API that handles XML

// Import Guzzle Client
use GuzzleHttp\Client;

// URL and XML VALUE
$url = 'YOUR URL;

$xml = '
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <YourFunction xmlns="YOUR URL">
            <YOURFIELD>[string?]</YOURFIELD>
            // Your other fields
        </YourFunction>
    </Body>
</Envelope>
';


$options = [
    'headers' => [
        'Content-Type' => 'text/xml; charset=UTF8'
    ],
    'body' => $xml
];

$client = new Client();

$response = $client->request('POST', $url, $options);

return $response->getBody();

// Response is normal

@isarantoglou for the mean time you can use that while we wait for answer on how to send it using Http

As you can see this doesn't work

// Import Http
use Illuminate\Support\Facades\Http;

// URL and XML VALUE
$url = 'YOUR URL;

$xml = '
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <YourFunction xmlns="YOUR URL">
            <YOURFIELD>[string?]</YOURFIELD>
            // Your other fields
        </YourFunction>
    </Body>
</Envelope>
';

$response = Http::withHeaders([
    'Content-Type' => 'text/xml; charset=UTF8'
])->post($url, [
    'body' => $xml
]);

return $response->getBody();

// Response is error

Logically it looks right, but I don't know why Http doesn't behave like how Guzzle/Client does it since Http is basically built on top of it.

isarantoglou's avatar

I ended up doing it with Guzzle (GuzzleHttp\Client) directly:

$client->post($url, [
                'body' => $xml ]);
1 like
mdalholm's avatar

@isarantoglou i had to do the same as you. tried different things but couldn't figure out why it didn't work.

danielrangelsa's avatar

Works for me!

$xml = '';

$response = Http::withHeaders(['Content-Type' => 'text/xml; charset=utf-8'])->send('POST', 'URL', [
            'body' => $xml,
        ]);

        $clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
        $cxml = simplexml_load_string($clean_xml);

        dd($cxml);
Erick Racancoj's avatar

I was able to send my xml request body successfully by doing the following:

use Illuminate\Support\Facades\Http;

Http::withHeaders([
    'Authorization' => $this->token,
])->send('POST', $this->url, [
    'body' => $this->documentoXml
]);
V1nk0's avatar

Tested with Laravel 8:

use Illuminate\Support\Facades\Http;

// you may need "application/xml" instead of "text/xml" as Content-Type
Http::withBody($xml, 'text/xml')
    ->withHeaders([
        'SOAPAction' => 'soap-action-goes-here',
    ])
    ->post($url);
3 likes
laracoft's avatar

I have the same issue with SOAP too, I added text/xml, SOAPAction as well as ->withDigestAuth(...) to no avail, still troubleshooting.

nhayder's avatar

is there is an update on how to use laravel HTTP client to post xml requests ???? i'm stocked with this thing since yesterday, nothing seems to fix the problem.

I tested my settings on postman and it worked but when i used it on laravel i keep on getting internal server error

 $url = 'https://test.......com';

        $xml = "
            <?xml version='1.0' encoding='ISO-8859-1' ?>
            <request>
                <item>ddd</item>
                ...
            </request>
            ";

	$response = Http::withBody(

            $xml, 'text/xml; charset=utf-8'

        )->post($url);

return $response;

Any ideas on how to solve this issue

ashleyevans's avatar

@nhayder

Hi,

I managed to get it working like this:

$body = file_get_contents('path/to/xml/data'); //should be able to just copy and paste instead of referencing a file

   return Http::withHeaders([
       'SOAPAction' => 'PLACEHOLDER_SOAP_ACTION',
   ])
   ->withBody($body, 'text/xml;charset=utf-8')
   ->send('POST', 'PLACEHOLDER_URL');```
7 likes

Please or to participate in this conversation.