Researchers have studied reciprocity as a motivation for users to share knowledge online. In this study, we focus on two different types of reciprocity as drivers of online contribution: ex post and ex ante reciprocity. Ex post reciprocity refers to users who receive help from others in the past, paying back by helping others. Using a quasi-experiment performed via the instrumental variable method as the identification strategy, we test whether users who receive more answers last week answer more questions in the current week on StackOverflow.com. We find a significant positive relationship between ex post reciprocity and knowledge contribution, and such a reciprocal motivation diminishes with time. Ex ante reciprocity refers to people helping others in expectation of future help from others. Using data from StackOverflow.com, we take advantage of a natural experiment with a difference-in-differences analysis and find evidence supporting the existence of ex ante reciprocity. This study offers a new taxonomy for reciprocity and new insights on how reciprocity drives online knowledge sharing.