Making Requests

Below are some example requests using different programming languages to get you started. All you need to do is build a URL and use the data from the response.

JavaScript Example

Chrome 3+ FF 3.5+ IE 8+ Safari 4+
// Set the URL
var url 'http://api.prosperent.com/api/search?api_key=<YOUR_API_KEY>&query=shoes&filterMerchant=6pm.com';

var urlparts url.split('?');

// Set the Request
var request = new XMLHttpRequest();

// For most browsers
if ('withCredentials' in request)
{
    request.open('POST'urlparts[0],  true);
    request.withCredentials  = true;
    request.setRequestHeader ('Content-type','application/x-www-form-urlencoded');
}

// For IE 8/9
elseif (typeof XDomainRequest !=  'POST')
{
    request = new XDomainRequest;
    request.open ('POST'urlparts[0]);
    request.onerror  = request.onprogress  = request.ontimeout  = function() {};
}

// Browser does not support CORS
else
{
    throw new Error('CORS not supported');
}

// Set the URL
var timeout setTimeout(function() { request.abort(); }, 30000);

// Set onload functionality
request.onload = function()
{
    clearTimeout(timeout);

    // Check for errors
    var errors JSON.parse (request.responseText).errors;
    if (errors.length  > 0)
    {
        throw new Error('Errors in API request');
    }

    // Loop through the data array
    var dataArray JSON.parse (request.responseText).data;
    for (var key in dataArray)
    {
        // Add your own code here to handle the response data
    }
}

// Send Request
request.send(urlparts[1] + '&origin=' + (document.URL  || document.location.href ));


PHP Example (PHP 4.0.2+)

This example requires the cURL Library.

<?php

// Set the URL
$url 'http://api.prosperent.com/api/search?api_key=<YOUR_API_KEY>&query=shoes&filterMerchant=6pm.com';

$curl curl_init();

// Set options
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_CONNECTTIMEOUT => 30,
    CURLOPT_TIMEOUT => 30
));

// Send the request
$response curl_exec($curl);

// Close request
curl_close($curl);

// Convert the json response to an array
$response json_decode($responsetrue);

// Check for errors
if (count($response['errors']))
{
    throw new Exception(implode('; '$response['errors']));
}

// Set specified data from response
$data $response['data'];

// Loop through the data array
foreach ($data as $key => $value)
{
    // Add your own code here to handle the response data
}

For other PHP implementations, refer to the following:



Python Example (Python 2.7.4+)

This example requires urllib2 for Python 2.7.4+ and urllib.request for 3.0+. It also requires the json module.

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
    import urllib.error as urllib2

except ImportError:
    # Fall back to Python 2 urllib2
    from urllib2 import urlopen
    import urllib2

import json
import socket

# Set the URL
url 'http://api.prosperent.com/api/search?api_key=<YOUR_API_KEY>&query=shoes&filterMerchant=6pm.com'

# Send the request
try:
    handle urlopen(url, timeout = 30).read().decode('utf-8')

# Catch any errors with the URL
except urllib2.URLError:
    raise  Exception("There was an error with the URL.")

# Catch timeout errors
except socket.timeout:
    raise  Exception("Request timed out.")

# Convert the json response to a dictionary
response json.loads(handle)

# Check for errors
if response['errors']:
    raise Exception
(response['errors'])

# Set specified data from response
data response['data']

# Loop through the data array
for result in data :

    # Add your own code here to handle the response data

For other Python implementations, refer to the following: