Creating & Updating Data

  Date Updated: Oct 2021

There are two request methods you can use to add and edit records in the database: HTTP PUT and POST.

HTTP PUT

Use the PUT method to update an existing entry in the database. For example, to update a customer contact record with new information you would use the PUT method.

Here's a C# sample using HTTP Web Request


using System;
using System.IO;
using System.Net;

class Test
{
   static void Main()
   {
       string json = "...";
       byte[] arr = System.Text.Encoding.UTF8.GetBytes(json);
       HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:8080/");
       request.Method = "PUT";
       request.ContentType = "text/json";
       request.ContentLength = arr.Length;
       Stream dataStream = request.GetRequestStream();
       dataStream.Write(arr, 0, arr.Length);
       dataStream.Close();
       HttpWebResponse response = (HttpWebResponse)request.GetResponse();
       string returnString = response.StatusCode.ToString();
       Console.WriteLine(returnString);
    }
}

HTTP POST

Use the POST method to add a new entry to the database, whether it already exists or not. For example, you might use the POST method to add a customer to the company file. If the customer you're adding already exists, another record for the customer will be added.

Here's a PHP example using cURL


// Initiate curl
$ch = curl_init();
// Where you want to post data
$url = "https://api.myob.com/";  // Company File or Business URI

// Headers
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);  // tell curl you want to post
// Setup authentication
curl_setopt($ch, CURLOPT_USERPWD, 'administrator' . ":");
// Define what you want to post
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"Your JSON":"here"}');
 // Return the output in string format
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute
$output = curl_exec ($ch);

curl_close ($ch); // Close curl handle

var_dump($output); // Show output

When to use?

Should I use PUT or POST?

If you want to:

  • add a new record, use the HTTP POST method.
  • edit a record that already exists within the company file, use the HTTP PUT method. This will update the content of the existing record.
  • add a record even if it already exists, use the POST method. This is known as a non-idempotent method. Repeating the POST call will add the record again.

Please note: When updating an existing record, you will need to add the unique record identifier GUID to the end of the URI