sribharath
sribharath

Reputation: 31

Getting results from api

I am trying to do a domain availability search using an API from free domain API.

After i create an account, it shows:

**Make a REST request using this URL:**
http://freedomainapi.com/?key=11223344&domain=freedomainapi.com

And looking in the documentation page, it has only:

Request http://freedomainapi.com?key=YOUR_API_KEY&domain=DOMAIN_NAME

Result:

{
"status": "success",
"domain": "freedomainapi.com",
"available": false
}

I am very new to APIs...

What I need is to show a domain search box, and when the user enters, it should return with result.

It claims to show domain suggestions as well. I hope it will also work.

Upvotes: 2

Views: 1327

Answers (6)

Jameel Mohammed
Jameel Mohammed

Reputation: 2364

on user enters, it calls click_button function and I am assuming your result displaying div id is "main_container" you can give domain suggestions by passing related DOMAIN_NAME s as arguments to click_button function

function click_button(DOMAIN_NAME){
$.ajax({
        url : 'http://freedomainapi.com?key=YOUR_API_KEY&domain=DOMAIN_NAME',
        type: 'GET',
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            data=JSON.parse(data);
            if(data['available']){
               $('#main_container').html($('#main_container').html()+'<br>'+DOMAIN_NAME+': Available');
            else{
                $('#main_container').html($('#main_container').html($('#main_container').html()+'<br>'+DOMAIN_NAME+': Not Available');

        }//success
       });//ajax
}

hope it helpful !

Upvotes: 0

manf
manf

Reputation: 338

Any API is a way to extend a given software. (Might be a website or an application)

In both ways there is a certain way to communicate with the software. In your example freedomainapi.com allows you to fetch if given domain is avaiable. There is no such thing as a suggestion tho, atleast i cannot find any suggestions at all.

Given output is a message format know as JSON. It can be easily interpreted by many major Languages such as Java, Javascript and PHP. Given String might be easily interpreted as a Map consisting of a status (String), a domain (string) and avaiable (boolean)

A domain availability search could not be easier, assuming K is your key, D is your search input (Domain):

  1. Download http://freedomainapi.com/checkAvailability?key=K&domain=D as input
  2. Parse JSON from input as json
  3. return json["status"] == "success" and json["avaiable"]

Depending on your language you might need to use methods to access properties of json, but that does not influence the basic usage of this api.

Upvotes: 0

G-Host
G-Host

Reputation: 376

Using jquery and a jsonp proxy

http://jsfiddle.net/mp8pukbm/1/

$.ajax({
    type: 'GET',
    url: "https://jsonp.nodejitsu.com/?callback=?",
    data: {url: 'http://freedomainapi.com?key=14ejhzc5h9&domain=freedomainapi.com'},
    dataType: "jsonp",
    success: myfn 
});
function myfn(data) {
   console.log(data);
}

you have to use the proxy because cross domain json is not permitted

EDIT: i made an update to show the result in a div (stringified) http://jsfiddle.net/mp8pukbm/2/

EDIT #2: i created a test key on that site, you have to use your own

EDIT #3: and there's your combo: http://jsfiddle.net/mp8pukbm/4/

Upvotes: 3

Abhishek
Abhishek

Reputation: 7045

If you are using JAVA as backend then you can use gson to parse the result, which is a json. After parsing you can read the values from result and display accordingly :)

Upvotes: 1

MyTwoCents
MyTwoCents

Reputation: 7622

Normally when we use REST we need to differentiate one REST call from another. Assuming this url

http://freedomainapi.com/checkAvailability?key=YOUR_API_KEY&domain=DOMAIN_NAME

In Application layer we need to write an interface

@GET
@Path("/checkAvailability")
@Produces({MediaType.APPLICATION_JSON})
public ReturnObject getDomainAvailability(@QueryParam("key") String key,
               @QueryParam("domain") String doaminName );

Once interface is done you need to write your implementation class. This class will intract with business layer and perform search task and based on result collected will create ReturnObject.

ReturnObject => will contain status, domain and availability

On screen

$.ajax({
        type: "GET",
        url: 'root/checkAvailability',
        success: function(jsonData)
        {                       
            // read json and perform operation 
        }                                       
        ,
        error: function (error)
        {
            // handle error 
        } 
});     

Upvotes: 1

Neeraj Krishna
Neeraj Krishna

Reputation: 1615

Assuming that you will use java script for showing the search box, you can use AJAX feature of java script (or jQuery or Dojo) ... All you need to do is a "GET" request that like you can pasted and you will get the result back on the response object. To try out the API you can use "Postman" application in Chrome. https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

In the response object of the AJAX call you will get a JSON object which you can parse and display the result.

Upvotes: 1

Related Questions