When to get a doctors note if I might have to miss an exam?
Check your course syllabus.  Often, policies for exam makeups are documented there.  It also might say here whether you need a doctor's note or not.
Contact your professor right away (by email or phone) to inform them that you are ill and may miss the exam.  As a professor, the further in advance I know about situations like this, the easier it is for me to make arrangements.  You could also ask at this time about whether you need a doctor's note (or confirm what you read in the syllabus), and/or explain the difficulties you would face in obtaining one.  The professor may be willing to make an exception.
Obviously, go to the doctor if you are in need of treatment.  Also go if the professor insists on having a note; this is probably not ideal if you don't otherwise need to see a doctor, but you really don't have much choice.
our i
Only you can make the decision as to whether you are "too sick" to take the midterm.  My feeling is that any of the following would be sufficient reasons to stay home:

you think you would significantly risk your health by attending
you have a contagious illness
your illness might disturb other students (coughing, running to restroom, etc)
you think your illness would significantly reduce your ability to do well on the exam (e.g. you are in pain, excessively fatigued, have distracting symptoms, etc)