October v2 HTTP class - How to post a json rawBody?

In a v2 October project, I am using the October\Rain\Network\HTTP class to post data to an external API.
The data are directly extracted using the method getAttributes() on my model which returned an array.
Some value are null, and the HTTP class do not convert them properly in a json null value, but leave the value empty which cause a validation error on the external API.

here is some of my code.

$data = $model->getAttributes();
$result = Http::post($endpoint, function ($http) use ($data) {      
   $http->data($data);
 });

$data looks like this

[requestData] => Array
        (
            [id] => 1
            [name] => ISO/TS 16949ss
            [slug] => isots-16949
            [description] => ISO/TS 16949
            [is_enabled] => 1
            [parent_id] => 
            [sort_order] => 1
        )

where parent_id should be the value null instead of being empty.

any idea how to get around that ?

or any way to send a raw body json query? I am thinking to override this HTTP class myself to give the capacity to send raw body request data but not sure what do to.

I tried to manipulate the body of the request directly, is this the proper way to do it?

$result = Http::post($endpoint, function ($http) use ($data) {
      $http->header('Content-Type', 'application/json');
      $http->body = json_encode($data);
      $http->rawBody = json_encode($data);
    });

Try setting CURLOPT_POSTFIELDS directly:

$this->setOption(CURLOPT_POSTFIELDS, 'raw body contents');

Make sure you do not use the data() method otherwise it will override this.

1 Like

this works, thank you @daft

1 Like