For the method attribute what are the differences between POST and GET?

In the form element, the method attribute can send data via either POST or GET. But I’m having trouble understanding which one to use and when? I tried to find an answer at w3schools, but just wasn’t getting it. Can someone give me a simple answer with examples?
Cheers,
Mark

1 Like

always use POST for sensitive data (login form, register form and so forth). POST will not display the information in the address/url bar.

get will display the information in the URL bar which means the page can be bookmarked (unlike post), get is useful for searching, lets say you have a search box, then get is a good idea.

148 Likes

Thank you for your explanation.

1 Like

So in the exercise example where they have used “GET” it is on a form which has username and password. would this not be classed as sensitive data? so why was “POST” not used?

1 Like

The exercise linked to this topic is:

https://www.codecademy.com/courses/learn-html/lessons/html-form-validation/exercises/minlength-maxlength

if that doesn’t work, its:

https://www.codecademy.com/learn/learn-html > lesson form validation > 4. checking text length.

The only example in this lesson:

<form action="/example.html" method="POST">
  <label for="summary">Summarize your feelings in less than 250 characters</label>
  <input id="summary" name="summary" type="text" minlength="5" maxlength="250" required>
  <input type="submit" value="Submit">
</form>

which a post.

So please clarify which exercise and example you are referencing.

But the topic you replied is related to a very different exercise then your question it seems. This topic is a split of this FAQ:

FAQ: Learn HTML: Form Validation - Checking Text Length

So please tell me which exercise this is.

you should use post, certainly with a login page and sensitive data like password and email.

1 Like

I think you may be seeing something completely different as i have definitely replied to the post "for the method attribute what are the differences between "post and “Get”

That is what this topic is named, but its related to an FAQ about a specific exercise:

image

and you are getting an example from a very different exercise, so I am trying to establish where your example is coming from

Because using GET for a login form is bad practice. You should use POST, that should cover your question

But if there is mis-information somewhere, I need to know where so it can be fixed.

As you will see “GET” is used the whole way through the fore mentioned “Form Validation lesson”

4 Likes

This lesson is not representative of a real world example. The validation happens the front-end. Although front-end validation an be useful to reduce the amount of server request and provide instant feedback, you always need back-end validation.

Which is not done in this lesson, I assume to not overwhelm new learners.

6 Likes

Please clarify… I happened to use the GET option and it didn’t show on the bar… I tried running this code on a different browser and vs code as my code editor…
how does one really tell the difference between POST AND GET

can I see what you did? I can’t solve a problem from such a vague description.

If you have anchor element:

<a href="https://discuss.codecademy.com">discuss</a>

clicking on the link will execute a GET request and change the url in the address bar.

ok… thank you very much for this moderation…

But we were talking about GET and POST as values of the method attributes…

Why was GET used instead of POST since we were dealing on password which is classified sensitive… You said that POST is for sensitive data to hide it from the address bar…

This exercise isn’t representative of a real world scenario, the form is handles in the front-end using JavaScript, where normally you would make a request to the back-end

and then GET is very likely used to simplify the process of getting the data using JavaScript.

3 Likes

Well,

  • Method get is for getting information to fetch realtime data to the users and
  • Method post for securing and storing data to a specific path or server in an encrypted way.
  • In a word, post for storing and get for results. Both get and post is used to dealing with data.
  • For example, if you submit your information in an e-commerce site, then you will input or submit your data as a POST method and if you search in Google for something to see as your output, then you use GET method.

Thanks.

13 Likes

Thank you so much…
I think I understand this as a beginner level

1 Like

thank you so much too for your contributions

1 Like

You are most welcome to do so . thank you.

1 Like

Thank you for explaining this. Just a thought (and I see where the confusion comes in), shouldn’t we be taught/prepped for the real world? Based on this lesson, I’m shown that ‘GET’ CAN be used in form validation, interchangeable with ‘POST’. It shows up in the code to the right of the lesson with no explanation (unless I’ve overlooked something), however the code works just the same (in the lesson). If I’m learning HTML for the first time, I was just shown an incorrect practice, was I not? Based on the responses given in this forum that is. Just, if I’m studying here to learn for the real world, then when I come to this forum or search google for more info on that topic, shouldn’t the information I find match the lesson and not contradict.

And THANK YOU again for the clarification! Huge help!

3 Likes

The real world is even more complex. A form might be a GET. For example if I had a form with an input field for a search, I would make the request GET, given get information is included the URL, which means you can share the page/search result. Validation would still be needed, making sure the search contains text/string, and not an image.

This just one example. What you should learn is: what the differences are, and what solution you should use in what situation and why

when sending sensitive data, POST should be used. Then the data is not included in the URL. Like create an account

However, given the codecademy exercise doesn’t actual make a back-end request (request is handled in front-end only, due to the extra layers of complexity introduced by the learning environment), the developer writing this exercise found it easier to read the data from the request using GET. Ideally, POST should have been used. But then, this exercise doesn’t handle the form validation properly like in the real world

6 Likes