Community articleSending a POST request in JavaScript
Added by IBM contributorIBM on September 27, 2013
Rate this article 1 starsRate this article 2 starsRate this article 3 starsRate this article 4 starsRate this article 5 stars

Here is sample code to send a POST request to the Domino® Data Service.
This particular request is a Document Collection POST das901Create New Article. It is coded in server-side JavaScript™ for XPages.

// Establish connection with Domino documents resource
var url = new java.net.URL(
	"http://ibm-z0shi2lw4cz.swg.usma.ibm.com:80/restTest.nsf/api/data/documents?form=Contact");
var conn:java.net.HttpURLConnection = url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
var writer = new java.io.OutputStreamWriter(conn.getOutputStream());
// Send JSON object as string
writer.write('{"FirstName":"Mary", "LastName":"MacBrown", "EMail":"mary.macbrown@acme.com", "City":"Glascow"}');
writer.close();
// Display the response - should be 201 for success
requestScope.status = conn.getResponseCode() + " " + conn.getResponseMessage();


Explanatory notes:
  • For a POST request, establish a network connection using java.net.HttpURLConnection, set the Content-Type request property to application/json, and set the Accept request property to application/json.
  • The body written to the connection should be a JSON object or array as a string.
  • The response code should be 201 for a successful POST.

Parent topic: Programming Domino Data Service