Announcement

Collapse
No announcement yet.

API POST (create entity)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • API POST (create entity)

    Dear support,

    We are using version 5.1.2.
    We are trying to create "Case" entity via API.

    In order to do it we try to perform the following (successfully passing through basic-auth):

    $.ajax({
    type: "POST",
    xhrFields: {
    withCredentials: true
    },
    beforeSend: function (xhr) {
    xhr.setRequestHeader('Authorization', 'Basic ' + btoa('our-userur-password'));
    },
    url: "https://our-url/EspoCRM-5.1.2/api/v1/Case",
    data: {
    name: "test"
    }
    }

    We do not see empty created "Case" in the system (in database as well).

    We tried to do as it's been stated in the API help however no Account has been created as well (blank response).

    $.ajax({
    type: "POST",
    xhrFields: {
    withCredentials: true
    },
    beforeSend: function (xhr) {
    xhr.setRequestHeader('Authorization', 'Basic ' + btoa('our-userur-password'));
    },
    url: "https://our-url/EspoCRM-5.1.2/api/v1/Account",
    data: {
    "name": "Test1",
    "assignedUserId": "1"
    }
    }

    Please help.



  • #2
    Hi,
    stringify data

    data: JSON.stringify({
    "name": "Test1",
    "assignedUserId": "1"
    })

    Comment


    • #3
      Many thanks for correction.

      Sorry about bothering on the same subject but
      we reworked into PHP however still having the same issue, please see below:

      function CallAPI($method, $url, $data = false)
      {
      $curl = curl_init();

      switch ($method)
      {
      case "POST":
      curl_setopt($curl, CURLOPT_POST, 1);

      if ($data)
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
      break;
      case "PUT":
      curl_setopt($curl, CURLOPT_PUT, 1);
      break;
      default:
      if ($data)
      $url = sprintf("%s?%s", $url, http_build_query($data));
      }

      // Optional Authentication:
      curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
      curl_setopt($curl, CURLOPT_USERPWD, "our_userur_password");
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

      $result = curl_exec($curl);

      file_put_contents("dump.txt", $url."\r\n".$data."\r\n".$result);

      curl_close($curl);

      return $result;
      }

      CallAPI(
      "POST",
      "https://our_site/EspoCRM-5.1.2/api/v1/Account",
      json_encode(
      [
      "name"=> "Test1",
      "assignedUserId"=> "1"
      ]
      )
      );


      "dump.txt" contains the following:

      https://our_site/EspoCRM-5.1.2/api/v1/Account
      {"name":"Test1","assignedUserId":"1"}


      However if we use GET - we can see the list of existing accounts in the system

      Please help

      Comment


      • #4
        a working example - https://forum.espocrm.com/forum/deve...empty-response

        Comment


        • #5
          Originally posted by tanya View Post
          Many thanks!
          Your example works for us - we will use it.

          Comment

          Working...
          X