Search code examples
phpjsoncodeigniter-4

how to post a raw data in CI4 with method curlrequest


Please what wrong in my code, I want to send send a raw data to API server. Sample raw data input in postman : { "LNTY_ID": 21, "LNG_DOC_NO": "LPY/DPS/I/22/017092", "REG_KODE": "PRE", "LNG_DATE": "2022-07-01"
}

and below is my code in ci4 :

public function uploadData()
{
    $client = \Config\Services::curlrequest();
    $headers = [];
    $data = [
        "LNTY_ID" => 21,
        "LNG_DOC_NO" => "LPY/I/22/017092",
        "REG_KODE" => "PRE",
        "LNG_DATE" => "2022-07-01"
    ];
    $url = "http://192.168.0.1/data_entry/";
    $response = $client->request('POST', $url, ['form_body' => $data, 'headers' => $headers, 'http_errors' => false]);
    echo $response->getBody();
}

When I run that code I get error message Bad Request.

Thank you for your help. Regards Nyoman


Solution

  • The option name is form_params, not form_body. https://codeigniter.com/user_guide/libraries/curlrequest.html#form-params