Rest API is one of the popular webservices in the world. This code implements Rest API for performing word count operation in a file, sorting array of numbers, checking whether a number is prime or not. For implementing this API we used flask python frame work. For converting the results in to json format, we used jsonify python library
Sorting array of numbers is required for so many applications. We are providing REST API service for sorting array of numbers. In this service we are going to pass query parameters to the http url. "array" parameter is of string data-type which we use to send array of numbers as string which are seperated by comma. Below is the request for sorting 34,7,8,9,1 array of numbers. you can use either GET or POST requests for the below url
http://ec2-35-160-1-123.us-west-2.compute.amazonaws.com/api/sort?array={array of numbers}
array: array of numbers divided by comma where we pass it as string data type
http://ec2-35-160-1-123.us-west-2.compute.amazonaws.com/api/sort?array=34,7,8,9,1
Response from server with header information
HTTP/1.1 200 OK
Date: Mon, 31 Oct 2016 17:23:48 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 24
Content-Type: application/json
{"result":[1,7,8,9,34]}
From the above it is clear that server response is in the form of json data type
- This service helps to check whether the number is prime or not
- Here we are using one query parameter "number" which is of integer data-type Below is the request for checking whether a number is prime or not. you can use either GET or POST requests for the below url
http://ec2-35-160-1-123.us-west-2.compute.amazonaws.com/api/prime?number={number}
number: number to be checked, which is of int data type
http://ec2-35-160-1-123.us-west-2.compute.amazonaws.com/api/prime?number=17
Response from server with header information
HTTP/1.1 200 OK
Date: Mon, 31 Oct 2016 18:52:33 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 26
Content-Type: application/json
{"result":"Prime Number"}
From the above it is clear that server response is in the form of json data type
- This service helps to find the frequency of word in a file
- Here you have to send text file and a word which you want to know the frequency in a file Below is the sample request using curl. In the below request we are uploading a file named "sample" and also sending "are" word to the server. Here we are finding the frequency of "are" word in sample text file. You can use either GET or POST requests for the below url
curl -i -X POST -F "file=@sample" -F "word=are" http://ec2-35-160-1-123.us-west-2.compute.amazonaws.com/api/wordfreq
file: Source file
word: Target word, which is of string data type
curl -i -X POST -F "file=@sample" -F "word=are" http://ec2-35-160-1-123.us-west-2.compute.amazonaws.com/api/wordfreq
Response from server with header information
HTTP/1.1 200 OK
Date: Tue, 01 Nov 2016 16:13:20 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 13
Content-Type: application/json
{"result":8}
From the above it is clear that server response is in the form of json data type