Is this story true? I can't remember where I read it.
No. This story is not found in any canonical text.
In his extensively researched and well-cited book1 - 'Hanuman's Tale: The Messages of a Divine Monkey', author Philip Lutgendorf says -
- Saved by the Name
This is another story concerning which Pandit Dube observed that, although absent from authoritative Sanskrit texts, it was ‘‘current
among enthusiastic devotees and oral storytellers (kathāvācaka)’’
(kalyāṇ 1975:339). Apparently it has remained so, for it is retold
in eight Hindi collections.
This story is mainly told to emphasize the unequivocal invincibility of the name of "Sītā - Rāma". As reiterated above, this story is not from any authoritative text.
Although may I add, that originally, Hanuman ji is not the target of god Rāma, and only when Hanuman gets in the way of Rāma (who was adamant about killing the king of Kashi), then Rāma is forced to 'try of kill' Hanuman ji, and not otherwise.
Author Lutgendorf further notes:
Some introduce minor modifications storyteller. One storyteller omits Hanuman’s mother and has Narada advise the king of
Kashi to seek shelter with Hanuman himself (H17:4748); another has the
showdown occur outside Anjana’s retreat and involve a real battle
between Rama and Hanuman, broken up by the arrival of Narada and
Vishvamitra, who arbitrate (H12:33–34). In Swami Prem’s version, the
offender is the gandharva Chitrasena, who accidentally spits betel
juice on the testy sage Durvasas, who complains to Rama; the rest of
the story follows the version I have given, except that Rama provides
Hanuman with a magic club to be used to defend those who repeat Rama’s
name (H18:276–84). In a more significant variant, the king is omitted
and Hanuman himself is tricked by Narada into insulting Vishvamitra,
the sage’s motive is to prove the superiority of the Name over its
bearer (nâma vs. nâmí; H13:216–17). Finally, one elaborate version
posits the ill will of Shani (Saturn), as the cause of the crisis.
Here, Rama and Lakshmana both pursue the hapless king, and Hanuman is
supposed to assist them. But when the king seeks Anjana’s protection,
Hanuman is faced with another dharma-saṅkat: to obey his master (and
surrogate father) Rama or his mother. Viewers of Hindi films will not
be surprised to learn that he chooses the latter course. As in the
Ahiravana story, Hanuman then coils his tail into a fortress in which
he places the king and before which he seats himself in meditation,
chanting the Name. Rama’s and Lakshmana’s weapons prove useless, and
ultimately Shiva intervenes to pacify them (H15: 245–47). The
storytellers’ usual choice of Vishvamitra as the offended character is
significant since he was once an arrogant Kshatriya who, by
superhuman effort, transformed himself into a Brahman, hence he
remains hypersensitive about receiving all the requisite prerogatives.
In all versions, the story celebrates the power of the Rama mantra
(being careful to include its three most common variants) and
Hanuman’s special relationship to it. Dube notes that the king has
been ‘‘initiated’’ (dıkshit) by Hanuman in the mantra and its use
(Kalyaṇ 1975:342), and this recalls the Ramanandi doctrine that
Hanuman is one of the great preceptors of the spiritual discipline of
‘‘remembrance of the Name’’ (nāma smaraṇa). Indeed, in one recent
version, when the king’s voice falters in his repetition, Hanuman
enters his throat and takes up the chant (H19:351–54). For his part,
Rama— who relentlessly pursues a status-based notion of ‘‘dharma’’
regardless of the cost—here seems to be fading into the cosmic
clockwork, to become just another of the adverse forces we humans are
likely to find aligned against us. Yet a remedy is at hand, in the
form of an intercessor who has managed to wrangle the boon of
defending us against adverse fate and even ‘‘God himself.’’
(page 224 & 225)
So this story is NOT true in reference to any authoritative Sanskrit scriptures.
The main aim of this folk-story is to expound on the famous Hindi saying - "Rāma se baḍā Rāma kā nāma" ("राम से बड़ा राम का नाम")
i.e., The name of the Lord is greater than the Lord himself.
1: Lutgendorf, Philip. Hanuman's Tale: The Messages of a Divine Monkey. United Kingdom: Oxford University Press, 2006.