DEV Community

Cover image for Exploring HTTP Protocol: Can You Send a GET Call with a POST Method ? and Vice Versa
Amrita-padhy
Amrita-padhy

Posted on

Exploring HTTP Protocol: Can You Send a GET Call with a POST Method ? and Vice Versa

In a recent web development job interview, I faced an unexpected question that led me to dive deep into HTTP methods. The interviewer asked: "Can you use the POST method to send a GET call, and vice versa?" This question caught me off guard, but it made me realize how crucial understanding HTTP methods is in web development.

In this blog post, I'll share my journey of exploring this question, from initial surprise to valuable insights gained through research and reflection. By understanding the nuances of HTTP methods, we can better grasp the basics of web development and API design.

GET Request with POST Method:

  • A GET request is typically used for fetching data from a server. It's meant to be idempotent, meaning making the same request multiple times should have the same effect as making it once.
  • POST requests, on the other hand, are intended for submitting data to the server to be processed. They may have side effects, such as updating a database or creating a new resource.
  • Sending a GET request with a POST method (by including parameters in the request body instead of the URL) violates the HTTP specification and can lead to confusion and unexpected behavior. It's not a recommended practice.

POST Request with GET Method:

  • Similarly, while you can technically send a POST request with a GET method (by including parameters in the URL instead of the request body), it's also not recommended.
  • GET requests are meant for retrieving data and should not have side effects on the server.
  • POST requests, on the other hand, are intended for actions that may change the state of the server or create new resources.

In both cases, using the wrong method for the intended purpose can lead to issues with interoperability, confusion, and unexpected behavior.

Therefore, it's essential to use the appropriate HTTP method based on the semantics of the request:

  • Use GET for retrieving data from the server.
  • Use POST for submitting data to the server to be processed.

While it's interesting to understand that HTTP methods are flexible in this way, it's crucial to adhere to best practices for clarity, consistency, and maintaining the integrity of web applications.

You can technically send a GET request with a POST method and vice versa, but it's generally not recommended or considered good practice

Top comments (1)

Collapse
 
franjruiz profile image
Fran

I agree that using "Post" to do "Get" and vice versa is not a good idea, but there are times when there is no choice. I'm thinking for example of OData services behind some WAF or other system that 'cuts off' requests with dangerous characters in the URL (for example with the classic SQL Injection pattern). In these cases, there might be no choice but to use a Post to pass the parameters in the message body so that the firewall doesn't "complain".
In any case, this is a good article to understand the differences between the two HTTP methods.