'Cannot convert http.post from php to dart

I tried to convert this code from php

<?php

function sendjson($url, $array_params)
{
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $bodyData = json_encode($array_params);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/x-www-form-urlencoded',
                'Content-Length: '.strlen($bodyData)
        ));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData);
        curl_setopt($ch, CURLOPT_POST, 1);
        $result = curl_exec($ch);
        $result = json_decode($result, true);
        return $result;
}

$idcontact=1068383;

$url="https://test.immo-lead.com/api/1/fullapi/fils_discussion/?apikey=05dec7e3707785bba8d3cd77666f9f35&customerkey=772329f7135be59973360cfe9b413d95";
$objet="test objet";
$tags="test tags";
$array_params = array(
            'idcontact' => $idcontact,
            'objet' => $objet,
            'tags' => $tags,
            'dateenr' => date('Y-m-d H:i:s')
        );
$result_api=sendjson($url, $array_params);
var_dump($result_api);

to dart

postDiscussion(idContact) async {
    var data = {
      'idcontact' : idContact, //1068383
      'objet' : objet.text,    //test objet
      'tags' : tags.text,      //test tags
      'dateenr' : DateTime.now().toString().substring(0,19) //2020-10-16 12:10:20
    };


    var res = await http.post('https://test.immo-lead.com/api/1/fullapi/fils_discussion/?apikey=05dec7e3707785bba8d3cd77666f9f35&customerkey=772329f7135be59973360cfe9b413d95', body: data);
    if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
    var body = jsonDecode(res.body);
    print(body);

  }

Both responded like this: {id: 277}. But code from dart only write NULL on database. I have no control on backend and database (my client give me his screenshot). I think may be the cause is I cannot convert function sendjson() to dart. But I don't know how to fix it.



Solution 1:[1]

You need to post data as an encoded string in the body. Do it like this:

postDiscussion(idContact) async {
    var data = {
      'idcontact' : idContact, //1068383
      'objet' : objet.text,    //test objet
      'tags' : tags.text,      //test tags
      'dateenr' : DateTime.now().toString().substring(0,19) //2020-10-16 12:10:20
    };

    String encodedData = json.encode(data);

    var res = await http.post('https://test.immo-lead.com/api/1/fullapi/fils_discussion/?apikey=05dec7e3707785bba8d3cd77666f9f35&customerkey=772329f7135be59973360cfe9b413d95', body: encodedData);
    if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
    var body = jsonDecode(res.body);
    print(body);
  }

You can read more.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akif