Your question relates to the concept of approach tones, where you precede a target "land tone" with an approach tone such as its leading tone half step below.
This happens all the time in the melodies of improvised solos and it happens, but less, in tune melodies (in jazz, this kind of melody is often called "head"). I suggest you pick a few classic standards (such as Cole Porter's or George and Ira Gershwin's) from the Real Book and have a look for yourself. The melodies there are often very simple, therefore useful to see how it's done.
So much for approach tones, I'll now move on to creating a melody that's consonant with the chords (which I think you're after). The considerations below are still connected to previous through the concept of land tone.
I feel that there's a concept more powerful than the approach tones one, which relates to the "voice leading" that takes place between the chord tones as one chord changes to the one that follows. I used quotes to highlight it's probably a good idea to use the term as a keyword for research.
The most important thing to watch here is the chord tones that "move" a half tone up or down. The most evident example of this I can offer is the chord G7 resolving on to the chord C, where the voice leading is the tone B "becoming" or "turning into" the tone C, and the tone F "becoming" the tone E.
Your melody following the chords by emphasizing voice leading is no requirement, but doing so makes your melody stick to the context created by the harmony, strongly. So much so that, if the chords change fast enough, your melody can use strong dissonance "against" chords and still get away with sounding "in".
You can create a melody that sounds very "inside" the harmony by choosing the targets of voice leading as land tones (among the existing chord tones). Approaching them just like the preceeding chord does (i.e. replicate the voice leading) is optional, you can achieve good consonance approaching your target in different ways.