I saw some similar questions in the website, namely here, here and here. What bothers me is that there is no general answer to this question:
When is it possible to transform a real integral to a contour integral? Always?
Or are there some requirements to satisfy, the shape of the function to integrate for example? And then, how do you decide the path to use?
I'm really confused about this. Can you point to any resource or keyword to search which may explain this in detail?
Most often when computing the real integral as a contour integral in the complex plane, one invokes Jordan's lemma.
$$C_R = \{R e^{i \theta} \mid \theta \in [0, \pi]\}$$ If the function $f$ is of the form $$f(z) = e^{i a z} g(z) , \quad z \in C_R $$ then Jordan's lemma states the following upper bound for the contour integral:
$$\left| \int_{C_R} f(z) \, dz \right| \le \frac{\pi}{a} M_R \quad \text{where} \quad M_R := \max_{\theta \in [0,\pi]} \left| g \left(R e^{i \theta}\right) \right| .$$
If $f$ is continuous on the semicircular contour $C_R$ for all large R and
$$\lim_{R \to \infty} M_R = 0$$ then by Jordan's lemma $$\lim_{R \to \infty} \oint_{C_R} f(z)\, dz = 0$$
Let $C=C_1\cup C_R$
where $C_1$ is the real line, and $C_R$ the half-circle.
We need
$$\oint_{C_R}f=0$$ for $$\oint_{C}f=\oint_{C_1}f=\int_{\mathbb{R}}f$$
to hold, resulting in
$$\int_{\mathbb{R}} f=2\pi i\sum\limits_{k=1}^nRes(f,z_k)$$
by residue theorem.
This is provided by Jordan's lemma in the limit as $R\to\infty$, assuming $\lim_{R \to \infty} M_R = 0$.