✨
Instagram APIs
✨
Create an User
Get User by Id
Create a Post
Get a Post by Id
Get all Posts by an User with Pagination
Setup:
STEP-1 : Install and Set Up MongoDB
https://docs.mongodb.com/v5.0/installation/
STEP-2 : Clone this repository
git clone https://github.com/aayush1607/instagram_api.git
cd instagram-api
STEP-3 : Run local MongoDB instance
mongo
STEP-4 : Download Dependencies
go mod download
STEP-5 : Finally Run the Server
go run main.go
STEP-6 : Then simply navigate in your browser or test the above mentioned APIs on Postman or CURL
APIs :
- Create an User
Method Allowed-POST
URLhttp://localhost:8080/users
JSON Request Body{ "Name":"aayush", "Email":"[email protected]", "Password": "aayush@1234" }
{ "InsertedID": "61617acff3b743f10bb0923a" }
- Get User By id
Method Allowed-GET
URLhttp://localhost:8080/users/?id=61617acff3b743f10bb0923a
JSON Response Body{ "_id": "61617acff3b743f10bb0923a", "name": "aayush", "email": "[email protected]", "password": "$2a$10$NI5Ub2L2jIePsJu8ljOjBeK.LtfdTj5OoaWV.G7XqU3zuhAF/inCS" }
- Create a Post
Method Allowed-POST
URLhttp://localhost:8080/posts
JSON Request Body{ "User": "61615ad9d9a12f1927083b7f", "Caption": "Hello Instagram 4", "Image_url": "www.insta.com", "Timestamp": "" }
{ "InsertedID": "61616540d9fbb9baedcff879" }
- Get a Post by id
Method Allowed-GET
URLhttp://localhost:8080/posts/?id=61616540d9fbb9baedcff879
JSON Response Body{ "_id": "61616540d9fbb9baedcff879", "user": "61615ad9d9a12f1927083b7f", "caption": "Hello Instagram 4", "image_url": "www.insta.com", "timestamp": "2021-10-09T09:47:44.93Z" }
- Get all Posts by user id with pagination
(limit is the number of posts per page and page is the page number you want to visit. If limit and page parameters are not provided then by default they will be taken as 1)
Method Allowed-GET
URLhttp://localhost:8080/posts/users/?id=61615ad9d9a12f1927083b7f&limit=2&page=1
JSON Response Body{ "posts": [ { "_id": "6161616aa66d684e5bb38fbc", "user": "61615ad9d9a12f1927083b7f", "caption": "Hello Instagram", "image_url": "www.insta.com", "timestamp": "0001-01-01T00:00:00Z" }, { "_id": "616161e885e8e66798b7ed78", "user": "61615ad9d9a12f1927083b7f", "caption": "Hello Instagram 2", "image_url": "www.insta.com", "timestamp": "2021-10-09T09:33:28.758Z" } ], "total": 4, "page": 1, "last_page": 2, "limit": 2 }
Postman API collection link
>Packages Used
- MongoDB Go Driver to work with mongodb database
- Bcrpyt to hash passwords
- net/http for response request and response writer
- encoding/json for encoding and decoding from json to type struct and vice versa
- time for saving current timestamps if not provided by frontend interface