search
HomeWeb Front-endJS TutorialHttp methods and codes

Http methods and codes The

HTTP method is used to indicate the operation of the API client that wants to perform a given resource. Each HTTP method corresponds to a specific operation, such as creating, reading, updating or deleting resources, and each request for the REST API must include the HTTP method.

The working principle of the HTTP protocol is that the client sends a request to the server, and the server responds to these requests. We send HTTP requests by using different HTTP methods (sometimes called HTTP verbs) to perform CRUD operations (creation, read, update, delete). Get and POST are the most commonly used HTTP methods, but there are more HTTP methods to learn. This article will introduce different HTTP methods and how to use them when constructing and using Web APIs.

The 9 HTTP methods you should understand

<.> 1. get method

If we want to retrieve data from resources (such as websites, servers, or APIs), we will send them GET requests. For example, if we want a customer list or specific customers, we will send a GET request to the server.

Since the GET method should not change the data on the resource, but only read them (read only), it is considered a security method. In addition, the get method is power.

How to use the GET method to test the API?

When we want to test the API, our most common method is the get method. Therefore, we expect the following situations:

If resources are accessible, the API will return 200 status code to indicate "OK".

In addition to the 200 status code, the server usually returns the response text in XML or JSON format. For example, we expect [/members] endpoint to return a list of members in XML or JSON format.

If the server does not support the endpoint, the server will return 404 status code, indicating "not found."

If we send a request with the wrong grammar, the server will return 400 status code to indicate "wrong request".

<.> 2. POST method

POST method creates new resources at the back end (server). The request text carries the data we want to send to the server. It is neither a security method nor a power. We do not expect the same results for each sending request. For example, the two same post requests will create two new equivalent resources with the same data and different resource IDs.

When sending a post request to the server, we expect the following situations: Ideally, if the POST request creates a new resource at the other end, the response should have the 201 state code, which means "created".

Sometimes the execution of the POST request will not return the resource in a given URL; in this case, this method will return 204 status code to indicate "no content."

How to test the post endpoint

Because the post method creates data, we must change the data carefully; it is strongly recommended to test all the post methods in the API. In addition, make sure to delete the created resources after the test is completed.

The following are some suggestions that we can use to test APIs that use the POST method:

Use the post method to create a resource, it should return the 201 state code.

Execute the GET method to check whether the resource is successfully created. You should get 200 status code and respond to the resource that has been created.

Using incorrect or incorrect data to execute the post method to check whether the operation fails.

<.> 3. Put method

Using the PUT request method, we can update the existing resources by sending the updated data as the content of the request text to the server. The PUT method updates the resources by completely replacing all it. If it is applied to a resource collection, it will replace the entire collection, so please use it carefully. The server will return 200 or 204 status code after successfully updating its existing resources.

How to use the PUT method to test the API?

PUT method is power, etc., it modifies the entire resource, so in order to test the behavior, we ensure the following operations:

Send a PUT request to the server many times, and it should always return the same result.

When the server completes the PUT request and updates the resource, the response should have a 200 or 204 status code.

After the server completes the PUT request, a GET request is issued to check whether the data on the resource has been updated correctly.

If the input is invalid or the format is error, the resources are not allowed.

<.> 4. Patch method

Patch is another unusual HTTP method. Similar to Put, Patch updates resources, but it only updates the data, not all updates. For example, for more accurate reasons, the request [Put] Customers/{Customerid} will completely update the fields in the Customers entity in the resources. However, the Patch method does update the fields provided by the customer entity. Generally, this modification should be standard format, such as JSON or XML. How to test the API with the Patch method?

To test the API by using the PATCH method, please follow the steps discussed in this article to test the API with the PUT and Post method. Consider the following results:

Send a PATCH request to the server; the server will return the 2xx HTTP status code, which means that the request has been successfully received, understood and accepted.

execute GET requests and verify whether the content has been updated correctly.

If the request is incorrect or the format is incorrect, the operation must fail.

<.> 5. DELETE method

As the name suggests, delete the resources. Delete method is power, no matter how many calls are called, it returns the same result.

Most APIs always return the 200 state code. Even if we try to delete the deleted resources, but in some APIs, if the target data no longer exists, the method call will return the 404 status code.

How to test the delete endpoint? When deleting some content on the server, we should be cautious. We are deleting data, which is important. First, make sure that delete data is acceptable, and then perform the following operations:

Call the post method to create new resources. Do not use actual data to test Delete. For example, create a new customer first, and then try to delete the customers you just created.

Send a delete request for specific resources. For example, request [Delete] /Customers /{Customer-Id} Delete customers with designated customer IDs.

Call the GET method for the deleted customers, and it should return 404 because resources no longer exist.

<.> 6. Head method

Head method is similar to the get method. But it does not have any response text, so if it returns the response text wrongly, it must be ignored. For example, [get] /Customers endpoint returns the customer list in its response text. In addition, [Head] /Customers also perform the same operation, but it does not return to the customer list. Before requesting GET endpoints, we can send the Head request to determine the size of the file or data we are downloading. Therefore, the head method is safe and power.

How to test the Head endpoint

One of the advantages of the Head method is that as long as the API supports it, we can test whether the server can be available and accessible, and it is much faster than the GET method because it does not respond to the text. We hope that the status code obtained from the API is 200. Before each other HTTP method, we can use the HEAD method to test the API.

<.> 7. Options method

We use this method to obtain information about possible communication options (allowed HTTP methods) to give in the server, or use the star number to refer to the entire server. This method is safe and power.

Various browsers use the Options method to check whether the CORS (transmitted resource sharing) operation on the target API is limited.

How to test Options endpoint

According to whether the server supports the Options method, we can use the Options method to test the number of fatal faults of the server. Try it, please consider the following points: Send an Options request and check the returned header and status code.

Test the failure of the resource that does not support the Options method.

<.> 8. Trace method

Trace method is used for diagnosis. It uses the same request text that the request textant to the server before the client to create a ring back test, and the successful response code is 200 OK. Trace method is safe and power.

Trace method may be dangerous because it may leak credentials. Hackers can use client attacks, including internal identity verification header.

How to use the Trace method to test the API?

Send a standard HTTP request, such as the GET request of /API /Status

Replace GET to Trace and send it again.

Check the content of the server. If the response has the same information as the original request, the trace function is enabled in the server and the work is normal.

<.> 9. Connect method

Connect method is used to establish end -to -end connections between clients and servers. It builds a two -way connection between them, just like a tunnel. For example, we can use this method to safely transmit large files between clients and servers.

HTTP method compare

Method Summary CRUD accepts the request of the request text power and other security Get obtain a single resource or resource group reading PUT one -time update of the entire resource update is yes Is it possible to create a new resource creation? Part of the renewal resource update of the patch Delete delete resources, is it or not? Options obtains the information about allowable operations, is it read? HEAD acquisition of the endpoint metadata read is it Trace is used to diagnose the purpose of reading Connect establishes a two -way connection between the client and resources -no-

The above is the detailed content of Http methods and codes. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.