LONDON — Boris Johnson, Britain’s new foreign secretary, has a quality unusual for a nation’s top diplomat: He can be spectacularly undiplomatic.

Mr. Johnson has suggested that President Obama had an “ancestral dislike of the British Empire,” written a poem insinuating that Turkey’s president had sexual relations with a goat, and likened the European Union — which he helped lead the campaign for Britain to leave — to Hitler’s Third Reich.

And that was only this spring.

In December, he compared Russia’s president, Vladimir V. Putin, to Dobby the House Elf, a “Harry Potter” character. In 2007, he wrote that Hillary Clinton looked like “a sadistic nurse in a mental hospital.” In 2002, he referred to Africans as “flag-waving pickaninnies.”

So it was with no little shock that the world reacted to the news Wednesday evening that Britain’s new prime minister, Theresa May, had named Mr. Johnson to lead the rarefied Foreign and Commonwealth Office, which employs 14,000 people in nearly 270 diplomatic offices and works with the secret intelligence service MI6.