Spritle Blog 04月08日 13:31
Manual API Testing with Postman – A Beginner-Friendly Guide
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文深入浅出地介绍了应用程序接口(API)的基础知识,包括其定义、工作原理和重要性。文章探讨了API的各种类型,重点讲解了REST API,并解释了客户端与服务器之间的区别。此外,还涵盖了API相关的术语,如URI、URL和URN,以及API中的特性和资源。最后,文章介绍了Postman这款流行的API测试工具,以及在Postman中创建和管理API请求的集合。通过本文,读者可以全面了解API,并掌握使用Postman进行API测试的基本技能。

💡API(应用程序接口)是一组规则和协议,允许不同的软件应用程序相互通信,充当中间人,使一个程序能够从另一个程序请求数据或服务。API的类型主要分为SOAP和REST,而REST API使用标准HTTP方法(GET、POST、PUT、DELETE)来实现客户端与服务器之间的交互,也就是我们常说的CRUD操作。

🌐在客户端-服务器架构中,客户端向服务器发送请求以访问或交互资源或服务。该架构可分为单层、两层、三层和N层架构,其中三层架构包括表现层、应用层和数据层。理解URI、URL和URN等术语有助于我们更好地识别和定位网络上的资源。

📦API中的关键概念包括特性和资源,特性指的是系统的特定功能或能力,而资源指的是可以通过API访问、操作或交互的任何实体或对象。在API交互中,有效载荷(Payload)是指随HTTP请求或响应发送的数据,分为请求有效载荷和响应有效载荷。

🛠️Postman是一款流行的API测试和开发工具,它允许用户向API发送请求并查看响应。用户可以在Postman中创建和管理API请求的集合,发送带有自定义标头、参数和正文数据的请求,以不同的格式查看响应,并使用预定义脚本自动化测试。

Introduction

APIs, or Application Programming Interfaces, are the invisible connectors that let different software systems talk to each other.They make it possible for apps to share data, perform actions, and deliver seamless user experiences.Whether you’re ordering food or checking maps, APIs are working behind the scenes.In this blog, we’ll explore what APIs are, how they work, and why they matter.By the end, you’ll have a clear understanding of how APIs power the apps we use every day.

What is an API ? 

For instance: Front-end of the app -> middle layer (API) -> Back-end of the app

The middle layer is used for both fetching data from the backend and inserting front-end data into the backend.

EXAMPLE: Imagine you order food on Swiggy or Zomato, and you can see the delivery partner moving on the map. Google Maps API acts like a helper, giving Swiggy/Zomato all the location and route details they need to keep you updated.

Types of API :

We can categorize APIs into many categories, but in most cases, APIs are categorized into two types: SOAP and REST.

Both are web services. (An API on the internet is called a web service, and all web services are APIs.)

A web service is an API wrapped in HTTP. A web service needs a network, while an API doesn’t need a network for its operation.

Understanding REST API

This is how REST APIs work: Request Message -> API -> Response Message.

Example: While we are searching for hotels on some website, it is the GET method; ordering food is the POST method; changing the food is the PUT method; and cancelling the order is the DELETE method.

Client vs Server: What’s the Difference?

The client is typically the user-facing component in a client-server architecture.

It handles requests from clients (such as browsers or apps), processes them, and sends back the required response or data.

Client -> Internet -> Server

CLIENT / SERVER ARCHITECTURE :

Client-Server Architecture is a model used in networking where two distinct entities, the client and the server, interact to provide services or resources.

The architecture is categorized as One-Tier Architecture, Two-Tier Architecture, Three-Tier Architecture, and N-Tier Architecture.

One-Tier Architecture (Single-Tier):

Two-Tier Architecture:

Three-Tier Architecture:

Example: A web application where the client interacts with a web server, and the server communicates with a database to fetch data.

Presentation Layer (HTML, CSS, JS) → Application Layer (Java, Python, C++, C#) → Data Layer (MySQL, PostgreSQL, MongoDB)

N-Tier Architecture:

An extension of the three-tier model, where there can be multiple intermediate layers for handling specific tasks (e.g., a separate authentication server, business logic server, etc.).
(Example: Large-scale enterprise applications with multiple specialized servers

Understanding API Terminologies: URI, URL, and URN

The basic terminologies we need to know about are URI, URL, and URN.

URI (Uniform Resource Identifier):

A URI is a unique string of characters used to identify a resource on the internet or within a system.
It provides a way to reference or locate a resource, whether it’s a webpage, image, file, or any other type of data.
Example:http://google.com/articles/articlename” – Google.com alone is the URI (just an identifier of the domain).

URL (Uniform Resource Locator):

A URL is a specific type of URI that not only identifies a resource but also provides the means to locate it on the internet.
It includes both the address of the resource and the protocol used to access it.
Example:http://google.com/articles/articlename” – The whole URL (full address with protocol and domain).

URN (Uniform Resource Name):

A URN is a specific type of URI that uniquely identifies a resource by name within a particular namespace, without providing the location or method of accessing that resource.
Unlike a URL, a URN does not include the address or protocol to access the resource. Instead, it provides a unique identifier for the resource.

Example:http://google.com/articles/articlename”articles/articlename is the URN, also known as the endpoint.

Features and Resources in APIs

Feature:

A feature generally refers to a specific characteristic, functionality, or capability of a system, application, or service.

Feature is the term used in manual testing to test some functionality.

Features are aspects that provide value to the user and define the system’s behavior or what it can do.

Example: In a messaging app, features might include sending messages, creating groups, audio calls, and file sharing.

Resource:

A resource refers to any entity or object that can be accessed, manipulated, or interacted with within a system. In the context of APIs or web services, a resource typically refers to data or objects that are made available through an API and can be created, read, updated, or deleted.

Example: Product could be a resource accessible at: https://api.example.com/products

Payload in APIs

Payload typically refers to the body of the HTTP request or response .

TYPES OF PAYLOAD

It is mainly categorized into two types such as Request Payload and Response Payload.

1.Request Payload: The data you send in the body of a request (e.g., when creating or updating a resource).

Example: Sending JSON data in a POST request to create one user:

jsonCopyEdit{  "username": "johndoe",  "email": "john@example.com"}2.Response Payload: The data sent back in the body of the response (e.g., when fetching data or confirmation of an action).Example: Response Payload of previously created user:jsonCopyEdit{  "status": "success",  "message": "User created successfully"}

Getting Started with Postman

We can do the following things in Postman:

    Creating and managing collections of API requests.Sending requests with custom headers, parameters, and body data.Viewing responses in different formats (JSON, XML, etc.).Automating tests with pre-defined scripts.

Try to create one workspace, and while creating it, we need to select which type we want and go with the API Testing option.

Collections in Postman

Major Requests we are going to use in the collection:

Delete – Delete existing resource from the database.

Understanding HTTP Status Codes

A status code is a three-digit number sent by a server in response to an HTTP request. It indicates the result of the request and whether it was successful, encountered an error, or requires further action.

200 Series:
200 – OK, 201 – Created, 202 – Accepted, 203 – Non-authoritative info, 204 – No content

400 Series:
400 – Bad request, 401 – Unauthorized, 403 – Forbidden, 404 – Not found, 409 – Conflict

500 Series:
500 – Internal server error, 501 – Not implemented, 502 – Bad gateway, 504 – Gateway timeout, 599 – Network timeout

Setting Up the Environment for API Testing

Pre-Requisites:

1.) NodeJS installation for creating a custom API

2.) Installing JSON Server

Then our custom API will run on localhost (you can see the port in CMD), and we can perform HTTP requests via Postman.

What is JSON ?

JSON is expanded as JavaScript Object Notation.

Terminologies:

Post Response Use Cases:

This section contains basic information and use cases about what we need to validate and know for Manual API Testing.

All the use cases and scripts are going to be executed in the Post-response tab in the ‘Scripts’ option.

Things we need to validate:-

    Status CodeHeadersCookiesResponse TimeResponse Body

Initial Level Validations and Use Cases:

Take this URL as an example: https://reqres.in//api/users?page=2

‘’https://reqres.in’’ –> Host or Domain
‘’api/users?’’ –> Path Parameters
‘’page=2’’ –> Query Parameters (after the question mark is known as a query parameter)
‘’2’’ –> Resource

In Postman, while sending requests in JSON format (inside the collection), we need to select the ‘Raw’ option under the Body section.

pm – It is a Postman library that we are going to use to write scripts.

To write a test script in Postman, we have two common functions: Normal Function and Arrow Function.

Normal Function Syntax:

javascriptCopyEditpm.test("Test Name", function() {      // assertion;  });

Arrow Function Syntax:

javascriptCopyEditpm.test("Test Name", () => {      // assertion;  });

In the “Test Name” field, we can write our required texts related to the action we are going to perform.

In the “assertion” field, we are going to write the actual script.

We will use this syntax in the Postman Scripts option while using HTTP requests in a collection.

1.Testing the single status code :-

javascriptCopyEditpm.test("status code is 200", () => {  pm.response.to.have.status(200);});

This syntax will test whether the HTTP response will return 200 as the status code or not.

We can change the status code as per our requirement in place of ‘200’.

2.Testing more than one status code :-

javascriptCopyEditpm.test("successful post request", () => {  pm.expect(pm.response.code).to.be.oneOf([200, 201]);});

This syntax will test whether the HTTP response will return either 200 or 201.

3.Testing Status Code Text :-

javascriptCopyEditpm.test("status code is 200", () => {  pm.response.to.have.status("created");});

It will check if the text of the status code is ‘created’ or not.

The post Manual API Testing with Postman – A Beginner-Friendly Guide appeared first on Spritle software.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

API REST API Postman HTTP
相关文章