DEV Community

Ega Prasetya
Ega Prasetya

Posted on

4 1

Golang Web - HTTP Method: GET & POST

A route handler usually accepts any type of request, in that sense: regardless of the type of HTTP method, it will still enter one handler (such as POST, GET, etc.).

PRACTICE

So learn and practice this code.

Alt Text

Struct *http.Request have a Method name material that can be used to check the method rather than the Request that`s in motion.

In the above example, the request to the route / method POST will generate the text POST output, while the method GET will produce the text GET output.

TEST

Use the POSTMAN or tools to encourage testing. Here is a sample of the request by Method GET.

Alt Text

While below is Method POST

Alt Text

If the method is instead of the GET and POST, then per the source code above, the request should have produced a response 400 Bad Request. Below are the example of the request by method PUT

Alt Text

So finally! Enjoy it.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay