Table API Java examples

Examples that demonstrate how to use the Table API with the Java language.

For these code samples, the following is assumed:
  • The project includes apache commons-httpclient.
  • The package declaration needs to be changed accordingly.
  • This is a sample code snippet that shows how to get started; it is not recommended as production-ready code.

In the examples, replace myinstance.service-now.com with the URL of your instance.

GET

package examples.rest.java;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class GetAction {
	public static void main(String[] args) throws IOException, HttpException {
 		GetAction restAction = new GetAction();
 		restAction.getRequest();
 	}
 
 	public void getRequest() throws HttpException, IOException {
 		CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(new HttpHost("instance.service-now.com")),
                new UsernamePasswordCredentials("WebServiceUser", "WebServiceUserPassword"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();

        try {
            HttpGet httpget = new HttpGet("https://instance.service-now.com/api/now/table/incident");
            httpget.setHeader("Accept", "application/json");
            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println(responseBody);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
 	}

}

POST

package examples.rest.java;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class PostAction {
	public static void main(String[] args) throws IOException, HttpException {
 		PostAction restAction = new PostAction();
 		restAction.postRequest();
 	}
 
 	public void postRequest() throws HttpException, IOException {
 	// This must be valid json string with valid fields and values from table
 	 		String postData = "{\"short_description\":\"Test with java post\"}";
 	 		CredentialsProvider credsProvider = new BasicCredentialsProvider();
 	        credsProvider.setCredentials(
 	                new AuthScope(new HttpHost("instance.service-now.com")),
 	                new UsernamePasswordCredentials("WebServiceUser", "WebServiceUserPassword"));
 	        CloseableHttpClient httpclient = HttpClients.custom()
 	                .setDefaultCredentialsProvider(credsProvider)
 	                .build();
 		
        try {
        	HttpPost httpPost = new HttpPost("https://instance.service-now.com/api/now/table/incident");
 	 		httpPost.setHeader("Accept", "application/json");
 	 		httpPost.setHeader("Content-Type", "application/json");
 	        HttpEntity entity = new ByteArrayEntity(postData.getBytes("utf-8"));
 	 		httpPost.setEntity(entity);
 	        
            System.out.println("Executing request " + httpPost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println(responseBody);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
 	}

}

PUT

package examples.rest.java;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class PutAction {
	public static void main(String[] args) throws IOException, HttpException {
 		PutAction restAction = new PutAction();
 		restAction.putRequest();
 	}
 
 	public void putRequest() throws HttpException, IOException {
 	// This must be valid json string with valid fields and values from table
 	 		String postData = "{\"short_description\":\"Test with java post\"}";
 	 		CredentialsProvider credsProvider = new BasicCredentialsProvider();
 	        credsProvider.setCredentials(
 	                new AuthScope(new HttpHost("instance.service-now.com")),
 	                new UsernamePasswordCredentials("WebServiceUser", "WebServiceUserPassword"));
 	        CloseableHttpClient httpclient = HttpClients.custom()
 	                .setDefaultCredentialsProvider(credsProvider)
 	                .build();
 		
        try {
        	HttpPut httpPut = new HttpPut("https://instance.service-now.com/api/now/table/incident/1231231");
 	 		httpPut.setHeader("Accept", "application/json");
 	 		httpPut.setHeader("Content-Type", "application/json");
 	        HttpEntity entity = new ByteArrayEntity(postData.getBytes("utf-8"));
 	 		httpPut.setEntity(entity);
 	        
            System.out.println("Executing request " + httpPut.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpPut);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println(responseBody);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
 	}

}

PATCH

package examples.rest.java;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class PatchAction {
	public static void main(String[] args) throws IOException, HttpException {
 		PatchAction restAction = new PatchAction();
 		restAction.patchRequest();
 	}
 
 	public void patchRequest() throws HttpException, IOException {
 	// This must be valid json string with valid fields and values from table
 	 		String postData = "{\"short_description\":\"Test with java post\"}";
 	 		CredentialsProvider credsProvider = new BasicCredentialsProvider();
 	        credsProvider.setCredentials(
 	                new AuthScope(new HttpHost("instance.service-now.com")),
 	                new UsernamePasswordCredentials("WebServiceUser", "WebServiceUserPassword"));
 	        CloseableHttpClient httpclient = HttpClients.custom()
 	                .setDefaultCredentialsProvider(credsProvider)
 	                .build();
 		
        try {
        	HttpPatch httpPatch = new HttpPatch("https://instance.service-now.com/api/now/table/incident/acdc4303d722310020c153b2b2520369");
 	 		httpPatch.setHeader("Accept", "application/json");
 	 		httpPatch.setHeader("Content-Type", "application/json");
 	        HttpEntity entity = new ByteArrayEntity(postData.getBytes("utf-8"));
 	 		httpPatch.setEntity(entity);
 	        
            System.out.println("Executing request " + httpPatch.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpPatch);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println(responseBody);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
 	}

}

DELETE

package examples.rest.java;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DeleteAction {
	public static void main(String[] args) throws IOException, HttpException {
 		DeleteAction restAction = new DeleteAction();
 		restAction.deleteRequest();
 	}
 
 	public void deleteRequest() throws HttpException, IOException {
 	// This must be valid json string with valid fields and values from table
 	 		
 	 		CredentialsProvider credsProvider = new BasicCredentialsProvider();
 	        credsProvider.setCredentials(
 	                new AuthScope(new HttpHost("instance.service-now.com")),
 	                new UsernamePasswordCredentials("WebServiceUser", "WebServiceUserPassword"));
 	        CloseableHttpClient httpclient = HttpClients.custom()
 	                .setDefaultCredentialsProvider(credsProvider)
 	                .build();
 	         	
        try {
        	HttpDelete httpDelete = new HttpDelete("https://instance.service-now.com/api/now/table/incident/acdc4303d722310020c153b2b2520369");
        	httpDelete.setHeader("Accept", "application/json");
 	        
            System.out.println("Executing request " + httpDelete.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpDelete);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
 	}
}