#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
HTTP

Methods in HTTP

HTTP methods, sometimes called verbs, tell the server what action the client wants to perform on a resource. The same URL, such as /api/tasks/7, can mean "read this task", "update it" or "delete it" depending on the method. Choosing the right method makes an API predictable, lets browsers and caches behave correctly and is the foundation of RESTful design.

The main methods and what they mean

MethodPurposeHas a body?SafeIdempotent
GETRead a resource or a listNoYesYes
POSTCreate a resource or trigger an actionYesNoNo
PUTReplace a resource completelyYesNoYes
PATCHChange part of a resourceYesNoNot guaranteed
DELETERemove a resourceUsually noNoYes
HEADLike GET but returns only headersNoYesYes
OPTIONSAsk which methods are allowed; used in CORS preflightNoYesYes

What safe and idempotent really mean

A safe method does not change server state. Browsers, crawlers and prefetchers assume they can call safe methods freely.

An idempotent method has the same effect whether it runs once or many times. Deleting task 7 twice leaves it deleted. Replacing it with PUT twice leaves the same final data. POST is not idempotent: submitting an order form twice may create two orders. That is why browsers warn you before resending a POST when you refresh, and why clients can safely retry PUT and DELETE after a timeout but should be careful with POST.

A RESTful set of routes

For a collection of tasks, a typical API maps methods to actions like this:

GET    /api/tasks        -> list tasks
POST   /api/tasks        -> create a task (returns 201 Created)
GET    /api/tasks/7      -> read task 7
PUT    /api/tasks/7      -> replace task 7
PATCH  /api/tasks/7      -> update some fields of task 7
DELETE /api/tasks/7      -> delete task 7 (often returns 204 No Content)

Calling them from the browser only changes the method and body in fetch:

await fetch('/api/tasks/7', {
  method: 'PATCH',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ done: true }),
});

await fetch('/api/tasks/7', { method: 'DELETE' });

PUT versus PATCH

The difference confuses many beginners. PUT sends the full new version of the resource; any field you leave out may be cleared. PATCH sends only the fields that change. If a task has a title, description and done flag, a PUT with only { "done": true } could wipe the title and description, while a PATCH with the same body changes just the flag.

Common mistakes with methods

Using GET for actions that change data is the most damaging. A link like /tasks/7/delete can be followed by a crawler, a link previewer or a browser prefetch, deleting data without anyone clicking. Changes belong in POST, PUT, PATCH or DELETE.

Another frequent issue is the HTML form limit. Plain forms only support GET and POST. To send PUT or DELETE, use fetch from JavaScript, or a method-override convention on the server.

Also be aware that sending a JSON body with a custom header, or using PUT, PATCH or DELETE, across origins triggers a CORS preflight OPTIONS request. If the server does not answer that OPTIONS request correctly, the real request never happens.

Summary points

  • GET reads, POST creates, PUT replaces, PATCH updates part, DELETE removes.
  • Safe methods change nothing; idempotent methods can be repeated safely.
  • Never change data with GET.
  • Pair each method with a fitting status code, covered in the next lesson.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course