Hi, basically 汉语 and 中文 means chinese language. They mean the same thing.
The difference lies in its literal translation.
汉语 literally means the Language of the Native Han Chinese People. When you say 汉语 you are referring to the language that is natively spoken by the Han Chinese people exclusively not other ethnic groups.
while 中文 literally means the text of the Language of China. When you say 中文,
you are referring to the language that any other ethnic groups and the Han Chinese people speak in general.
However, "中文" would be used outside of mainland China since overseas han chinese and foreigners outside of China can speak and understand mostly standard chinese in general but not much of Han Chinese Dialects spoken in Mainland China. But in mainland China, you use 普通话, because Han Chinese in mainland China also speak Han Chinese Dialects in their respective provinces which are variants from the Standard Han Chinese that you know, so if you say "普通话", in mainland china the mainland chinese would know that you are refering to the standard chinese language that the majority of the mainland Han Chinese speak and understand regardless of province.