Musnad Ahmad 637, 638
It was narrated that `Ali (رضي الله عنه) said:
The Messenger of Allah (ﷺ) passed by me when I was in pain and I was saying: O Allah, if my time has come then grant me relief, if it has not yet come then raise me in status, and if this is a trial then grant me patience. He said: “What did you say?” I repeated it to him, then he nudged me with his foot and said: `What did you say?` I repeated it to him and he said: “O Allah, grant him well being or heal him.” And I never suffered that pain again after that.

It was narrated that `Ali (رضي الله عنه) said: I was ill and the Messenger of Allah (ﷺ) passed by me... And he mentioned a similar report, except that he said: “O Allah, grant him well being; O Allah, heal him.”
حَدَّثَنَا يَحْيَى، عَنْ شُعْبَةَ، حَدَّثَنَا عَمْرُو بْنُ مُرَّةَ، عَنْ عَبْدِ اللَّهِ بْنِ سَلَمَةَ، عَنْ عَلِيٍّ، رَضِيَ اللَّهُ عَنْهُ قَالَ مَرَّ بِي رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ وَأَنَا وَجِعٌ وَأَنَا أَقُولُ اللَّهُمَّ إِنْ كَانَ أَجَلِي قَدْ حَضَرَ فَأَرِحْنِي وَإِنْ كَانَ آجِلًا فَارْفَعْنِي وَإِنْ كَانَ بَلَاءً فَصَبِّرْنِي قَالَ مَا قُلْتَ فَأَعَدْتُ عَلَيْهِ فَضَرَبَنِي بِرِجْلِهِ فَقَالَ مَا قُلْتَ قَالَ فَأَعَدْتُ عَلَيْهِ فَقَالَ اللَّهُمَّ عَافِهِ أَوْ اشْفِهِ قَالَ فَمَا اشْتَكَيْتُ ذَلِكَ الْوَجَعَ بَعْدُ
حَدَّثَنَا عَفَّانُ حَدَّثَنَا شُعْبَةُ عَنْ عَمْرِو بْنِ مُرَّةَ قَالَ سَمِعْتُ عَبْدَ اللَّهِ بْنَ سَلَمَةَ عَنْ عَلِيٍّ رَضِيَ اللَّهُ عَنْهُ قَالَ كُنْتُ شَاكِيًا فَمَرَّ بِي رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ فَذَكَرَ مَعْنَاهُ إِلَّا أَنَّهُ قَالَ اللَّهُمَّ عَافِهِ اللَّهُمَّ اشْفِهِ فَمَا اشْتَكَيْتُ ذَلِكَ الْوَجَعَ بَعْدُ‏.‏