He later obtained naturalization as a British citizen, and entered the military service of the East India Company.In 1861 he was appointed an assistant commissioner in the Punjab region of British India, and in 1877 became deputy commissioner of Peshawar (now in Pakistan) and took part in several expeditions against the Pathan tribes.
In September 1878 he was attached to the staff of a British mission to Kabul, Afghanistan, which the Afghans refused to accept on their soil. In May 1879, after the British-Indian forces had invaded Afghanistan, and the death of Afghan Emir Sher, Cavagnari negotiated and signed the Treaty of Gandamak with Sher Ali's son and successor, Yaqub Khan. With this treaty, the Afghans agreed to admit a British representative to Kabul. The post was conferred on Cavagnari. He took up his residence in July 1879. However, on 3 September 1879, Cavagnari and the other European members of the mission, along with their guards who were made up of soldiers of the Corps of Guides, were killed after Afghan troops mutinied against them.