Lack of caffeine causing horrible headaches

Can I drink coffee when fasting? At first I thought the headache was from a lack of food. But I ate and still had a terrible headache the entire day. Now I’m attributing the headache to lack of caffeine.

If I drink my normal black coffee how can I counteract an upset stomach?