API » Code Snippet
Use the code snippets below to generate services compatible with Entrata' API in .NET, Java, or PHP.
/* An Example XML Request */
string xmlRequest = '
<request>
<auth>
<type>basic</type>
</auth>
<method>
<name>required_service_name</name>
<params>
use the parameters required for the web service here
</params>
</method>
</request>';
/* An Example JSON Request */
string jsonRequest = '
{
"auth": {
"type" : "basic"
},
"method": {
"name": "required_service_name",
"params": {
use the parameters required for the web service here
}
}
}';
/* Initiate a Web Request object */
HttpWebRequest webRequest = null;
webRequest = WebRequest.Create(' REPLACE THIS WITH THE WEB SERVICE URL ') as HttpWebRequest;
webRequest.Method = 'POST';
webRequest.Headers.Add("Authorization", "Basic <BASE64 ENCODED VALUE OF USERNAME:PASSWORD>");
/* Initiate the request writer */
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
/* If you want to send an XML Request, use these options */
webRequest.ContentType = 'APPLICATION/XML; CHARSET=UTF-8';
requestWriter.Write(xmlRequest);
/* If you want to send an XML Request, use these options */
webRequest.ContentType = 'APPLICATION/JSON; CHARSET=UTF-8';
requestWriter.Write(jsonRequest);
requestWriter.Close();
/* Read the response */
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
/* An Example XML Request */
string xmlRequest = '
<request>
<auth>
<type>basic</type>
</auth>
<method>
<name>required_service_name</name>
<params>
use the parameters required for the web service here
</params>
</method>
</request>';
/* An Example JSON Request */
string jsonRequest = '
{
"auth": {
"type" : "basic"
},
"method": {
"name": "required_service_name",
"params": {
use the parameters required for the web service here
}
}
}';
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
public class PostExample {
public static void main(String[] args){
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost("REPLACE THIS WITH THE WEB SERVICE URL");
request.addHeader("Authorization", "Basic <BASE64 ENCODED VALUE OF USERNAME:PASSWORD>");
/* If you want to send an XML Request, use these options */
StringEntity params = new StringEntity(xmlRequest);
request.addHeader("content-type", "APPLICATION/XML; CHARSET=UTF-8");
request.setEntity(params);
/* If you want to send an JSON Request, use these options */
StringEntity params = new StringEntity(jsonRequest);
request.addHeader("content-type", "APPLICATION/JSON; CHARSET=UTF-8");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
// handle response here...
System.out.println(response);
}catch (Exception ex) {
// handle exception here
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}
/* An Example XML Request */
$xmlRequest = '
<request>
<auth>
<type>basic</type>
</auth>
<method>
<name>required_service_name</name>
<params>
use the parameters required for the web service here
</params>
</method>
</request>';
/* An Example JSON Request */
$jsonRequest = '
{
"auth": {
"type" : "basic"
},
"method": {
"name": "required_service_name",
"params": {
use the parameters required for the web service here
}
}
}';
/* Initiate a CURL resource */
$resCurl = curl_init();
/* If you want to send an XML Request, use these options */
curl_setopt( $resCurl, CURLOPT_HTTPHEADER, array( 'Content-type: APPLICATION/XML; CHARSET=UTF-8', 'Authorization: Basic <BASE64 ENCODED VALUE OF USERNAME:PASSWORD>' ) );
curl_setopt( $resCurl, CURLOPT_POSTFIELDS, $xmlRequest );
/* If you want to send a JSON Request, use these options */
curl_setopt( $resCurl, CURLOPT_HTTPHEADER, array( 'Content-type: APPLICATION/JSON; CHARSET=UTF-8', 'Authorization: Basic <BASE64 ENCODED VALUE OF USERNAME:PASSWORD>' ) );
curl_setopt( $resCurl, CURLOPT_POSTFIELDS, $jsonRequest );
curl_setopt( $resCurl, CURLOPT_POST, true );
curl_setopt( $resCurl, CURLOPT_URL, REPLACE THIS WITH THE WEB SERVICE URL );
curl_setopt( $resCurl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec( $resCurl );
if( false === $result ) {
echo 'Curl error: ' . curl_error( $resCurl );
curl_close( $resCurl );
} else {
curl_close( $resCurl );
echo $result;
}