I've just begun learning how to integrate and I wanted to see if I could integrate $\sec(x)$ by $u$-substitution. After getting my answer, I was told it couldn't be in complex form, but why, and if so, where did I go wrong? Here is how I did it: $$\int \sec(x) dx$$ $$\int \frac{1}{\cos(x)} dx$$ $$\int \frac{1}{\frac{e^{ix}+e^{-ix}}{2i}}dx$$ $$\int \frac{2i}{e^{2ix}+1}dx$$ $u=2ix,du=2idx$ $$\int \frac{1}{e^u+1}du$$ $$\int \frac{e^u}{e^u(e^u+1)}du$$ $v=e^u,dv=e^udu$ $$\int \frac{1}{v^2+1}dv$$ $[\arctan(v)]$
$[\arctan(e^u)]$
$\arctan(e^{2ix}) + C$
Going from your $3$rd line to the $4$th, you have multiplied the denominator by $e^{ix}$ but not the numerator. Thus, your work after that will not be correct. Also, in general, as the original question is in reals, your final answer should also be only in reals, with this indicating why your final answer can't be correct.
Note I saw that Eevee Trainer just wrote about the mistake in the comments, along with a more detailed explanation.