json_fetch
json_fetch
This API allows developers to interact with [Your App Name] for:
All API requests start with the following base URL: https://api.example.com/v1
Endpoints:
● Each endpoint represents a specific functionality (e.g., fetching items or adding a new item).
● Simply append the endpoint to the base URL.
Request Format:
● Use tools like Postman or code-based HTTP libraries like fetch (JavaScript) to send requests.
HTTP (HyperText Transfer Protocol)
HTTP (HyperText Transfer Protocol) is the foundational communication protocol used on the internet to
transfer data between clients (like web browsers or apps) and servers.
Whenever you open a website (like google.com), your browser sends an HTTP request to the server where
the website is hosted, and the server replies with an HTTP response.
Key Features of HTTP:
1. Client-Server Model:
○ The client sends requests (e.g., asking for a webpage).
○ The server processes the request and sends back a response (e.g., the webpage content).
2. Stateless:
○ Each request is independent of the previous one.
○ The server does not remember any information about past interactions.
3. Text-Based Protocol:
○ HTTP uses plain text for communication, making it human-readable.
How HTTP Works:
1:Request: The client sends an HTTP request to a server.
● Request Line: Contains the HTTP method (GET, POST, etc.), the URL, and the HTTP
version.
● Headers: Extra information like Content-Type or User-Agent.
● Body: Data sent with the request (e.g., form data in a POST request)
2:Response: The server processes the request and sends back an HTTP
response.
● Status Line: Indicates success or failure (e.g., 200 OK or 404 Not Found).
● Headers: Additional information (e.g., Content-Type or Content-Length).
● Body: The actual data (e.g., HTML content or JSON data)
What is inside HTTP?
Think of HTTP as a package you send across the internet.
It can be a Request (you asking for something) or a Response (the server replying to you).
Whether it’s a request or a response, the HTTP message usually has 3 main parts:
📦 Inside an HTTP Message
1. Start Line
2. Headers
3. Body (optional)
1. 🚀 Start Line
This tells what’s happening in the message.
Content-Type: application/json
The body is the actual content or data you are sending with your HTTP request or response.
● In GET requests, there’s usually no body — you’re just asking for something.
● In POST, PUT, or PATCH, you usually include a body, because you are sending data to the server.
⚠ What if you use the wrong Content-Type?
Content-Type: text/plain
{"name": "John"}
“Wait, I was expecting plain text but this looks like JSON. I don’t know what to do with it.”
Result: You get a 400 Bad Request error.
HTTP Methods:
2:Language Independent:
● While JSON originated from JavaScript, it is language-independent and can be used by most
modern programming languages, including Python, Java, Ruby, PHP, and more.
3:Structured Data:
● JSON organizes data in a key-value pair format, which is intuitive for representing objects or
structured data.
4:Easy to Parse:
● JSON is natively supported by JavaScript, and most languages have libraries to parse and
generate JSON data easily.
JSON Syntax and Structure
● To Convert JSON String to JavaScript Object: JSON is typically transmitted as a string, but
we need a JavaScript object to easily manipulate the data.
● For Working with the Data: Once the data is in object form, you can access its properties and
manipulate it like any other JavaScript object.
2. Stringifying JavaScript Objects (Converting JavaScript Object to JSON String)
When you need to send data from JavaScript to a server (for example, in an HTTP POST
request), you often need to convert a JavaScript object into a JSON string. This is where
JSON.stringify() is used.
● To Convert JavaScript Object to JSON String: Servers often expect data in JSON
format, so we need to turn our JavaScript object into a JSON string before sending it.
● For Sending Data to APIs: When making API requests with POST or PUT, the data is
typically sent as a string in the body of the request.
Why do we need both JSON.parse() and JSON.stringify()?
Data comes as a string: When you receive data from a server (like in an API
response), it’s usually in the form of a string. But to use it in JavaScript (e.g.,
accessing properties like name, age), it needs to be converted into a JavaScript
object using JSON.parse().
Data needs to be a string for sending: Similarly, when sending data to a server, we
need to convert JavaScript objects into strings using JSON.stringify() because
servers expect the data in a string format (typically JSON).
These two methods are essential when working with APIs because they allow for
seamless conversion between the data format used by the server (JSON) and the
format used by JavaScript (objects).
Understanding fetch() in JavaScript
fetch() is a modern JavaScript method for making network requests, such as retrieving data
from an API or sending data to a server. It is a part of the Fetch API and provides a clean and
simple interface for working with HTTP requests.
What is fetch()?
Parameters:
1. url: The endpoint or URL you want to make the request to (e.g., API endpoint).
2. options (optional): An object containing request configuration such as method,
headers, body, etc.
Returns:
1. url (Mandatory)
The url parameter is the address of the resource or endpoint you want to interact with,
such as an API or a file on a server. This is the only mandatory parameter in the fetch()
method.
2. options (Optional)
The options parameter is an object that allows you to customize the request. It
specifies the HTTP method, headers, body, mode, cache, and other configurations for
the request.
Common Properties in the options Object:
1:method (Optional):
fetch('https://api.example.com/users', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer myToken123'
}
});
Understanding Content-Type Header
The Content-Type header is used to specify the format of the data being sent in the body of a
request or being received in the response. It acts as a communication bridge between the client
(you) and the server to ensure both sides understand the type of data being exchanged.
● Why Use It? JSON is lightweight, easy to read, and supported by most programming
languages. It’s the standard format for exchanging data between clients and servers.
headers: { 'Content-Type': 'application/json' // Tells the server you're sending JSON
},
How It Works:
1. The Content-Type: application/json tells the server, "Hey, I’m sending JSON data."
2. The server knows to parse the body as JSON (not plain text or some other format).
3. Without this header, the server might fail to parse the data or throw an error.