What is HTTP Methods and its types?

HTTP (Hypertext Transfer Protocol) is a protocol for transmitting data over the internet. It defines a set of methods that indicate the desired action to be performed on a specific resource.

The most commonly used HTTP methods are:

GET: Retrieves information about a resource. 

Example:


import requests

response = requests.get("https://jsonplaceholder.typicode.com/users/1")
print(response.json())


POST: Submits information to be processed by the resource identified by the URI. 

Example


    import json
    import requests

    data = {
        "name": "John Doe",
        "email": "johndoe@example.com"
    }

    headers = {'Content-type': 'application/json'}
    response = requests.post("https://jsonplaceholder.typicode.com/users",
data=json.dumps(data), headers=headers)
    print(response.status_code)



PUT: Replaces all current representations of the target resource with the request payload. 

Example

 
  import json
    import requests

    data = {
        "name": "John Doe",
        "email": "johndoe@example.com"
    }

    headers = {'Content-type': 'application/json'}
    response = requests.post("https://jsonplaceholder.typicode.com/users",
data=json.dumps(data), headers=headers)
    print(response.status_code)


PATCH: Partially updates a resource. 

Example

   
import json
    import requests

    data = {
        "email": "johndoe@example.com"
    }

    headers = {'Content-type': 'application/json'}
    response = requests.patch("https://jsonplaceholder.typicode.com/users/1",
data=json.dumps(data), headers=headers)
    print(response.status_code)


DELETE: Deletes a resource. 

Example

   
import requests

    response = requests.delete("https://jsonplaceholder.typicode.com/users/1")
    print(response.status_code)


HEAD: Same as GET, but only returns the headers, not the resource itself. Example: HEAD /users/123

OPTIONS: Describes the communication options for the target resource. Example: OPTIONS /users

CONNECT: Establishes a tunnel to the server identified by the target resource.

TRACE: Performs a message loop-back test along the path to the target resource.

The last one is HTTP Method is HTTP2 (HTTP/2) is a major revision of the HTTP network protocol used by the World Wide Web. 

It was developed to reduce the latency and overhead of HTTP.




























Mudasir Abbas Turi

Hi, this is Mudasir Abbas Turi. I am a Full-stack PHP and JavaScript developer with extensive experience in building and maintaining web applications. Skilled in both front-end and back-end development, with a strong background in PHP and JavaScript. Proficient in modern web development frameworks such as Laravel and ReactJS. Proven ability to develop and implement highly interactive user interfaces, and to integrate with various APIs and databases. Strong problem-solving skills and ability to work independently and as part of a team. Passionate about staying up-to-date with the latest technologies and industry trends.

Post a Comment

Previous Post Next Post