🎁Sending Body/Data

When sending HTTP requests, a common requirement is to send data to the server with POST, PUT, or PATCH requests, like JSON, XML or multipart data. Saloon makes this easy for you with built-in body traits.

Getting Started

To get started, you will need to add the HasBody interface to your request. This interface is required as it tells Saloon to look for a body() method supplied by one of the body traits. Without this interface, Saloon will not send any request body to the HTTP client. Also make sure to change your method to POST, PUT or PATCH depending on the requirements of the API.

<?php

use Saloon\Http\Request;
use Saloon\Contracts\Body\HasBody;

class CreateServerRequest extends Request implements HasBody
{
    protected Method $method = Method::POST;
}

Next, you will need to add a trait to provide an implementation for the missing body() method. Saloon has a trait for all the common types of request bodies.

Continue reading below to understand more about the specific body type that you need.

pageJSON BodypageMultipart Form BodypageStream BodypageURL Encoded Body (Form)pageXML BodypageString / Plain Text Body

Last updated