Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make json api

Sorry if this is a basic question. But I am planning to make an api for my website. First this api will be used by me but later I will release for the developers to use it. I am confused, this website is going to be used for both mobile apps and for websites. I am planning to make android app for this.

The question is do I have to make this api in different languages i.e. in Java for android plateforms, in Objective C for iOS and javascript and php for web?

Will it work for several platforms if I only make this in javascript?

like image 528
Om3ga Avatar asked May 18 '12 06:05

Om3ga


People also ask

What is @JSON API and how to use it?

JSON API inspects entity type and bundle to provide URLs to access each one of them using the standard HTTP methods, GET, POST, PATCH, and DELETE (we will discuss more on HTTP methods while talking about Document Structure). JSON is not simply a format like JSON or HAL+JSON. The default format appears like:

How do I create a JSON request in Python?

Creating the Request JSON I recommend using Postman in most cases, depending on the complexity of the API. If the JSON syntax is straightforward, you can format your data as a python dictionary, then convert it to a JSON object with json.dumps from the standard library’s json module. But JSON can be tricky sometimes.

How do I start a JSON server in Node JS?

Installing JSON Server JSON Server is available as a NPM package. The installation can be done by using the Node.js package manager: Now let’s create a new JSON file with name db.json. This file contains the data which should be exposed by the REST API. Let’s start JSON server by executing the following command:

How to install&configure JSON server for REST API?

JSON Server is available as a NPM package. The installation can be done by using the Node.js package manager: By adding the -g option we make sure that the package is installed globally on your system. Now let’s create a new JSON file with name db.json. This file contains the data which should be exposed by the REST API.


1 Answers

On the server side, your JSON API would be written in one language on one platform, this could be PHP, .NET or any platform of your choice.

On the client side (iPhone, Android, etc) You would need to write a client that is capable of making requests and handling responses to your JSON API.

However, in order to enforce consistency across your client API's you can employ a pattern, such as the request-reply pattern, I use this all the time as its easy to use and to implement.

The idea is for every JSON API method, you have a Request class, and a Response class. You would also write a service client that represents your JSON API.

An example

Let's say I have a JSON service that gives me contact details from my address book, it could have these service methods:

/contact/{id}
/address_book
/save_contact/{id}

My service client (example in Java) could have this interface:

public interface AddressBookClient {
    public GetContactResponse getContact(GetContactRequest request);
    public GetAddressBookResponse getAddressBook(GetAddressBookRequest request);
    public SaveContactResponse saveContact(SaveContactRequest request);
}

Although the implementation would be different across client platforms, using the same approach or pattern would keep them consistent.

Hope that helps.

like image 93
Ian Warwick Avatar answered Sep 28 '22 14:09

Ian Warwick