We used to go on military expeditions with the Messenger of Allah ﷺ and we had nothing with us. We said, "Should we not castrate ourselves?" But he forbade us from doing so. Then he permitted us to marry a woman in exchange for a garment as her dowry, and he recited to us, "O you who believe, do not forbid the good things that Allah has made lawful for you, and do not transgress. Indeed, Allah does not love the transgressors" [Maidah 5:87].
كنا نغزو مع رسول الله ﷺ وليس لنا شيء، فقلنا: ألا نستخصي؟ فنهانا عن ذلك، ثم رخص لنا أن ننكح المرأة بالثوب، ثم قرأ علينا {يأيها الذين ءامنوا لا تحرموا طيبات ما أحل الله لكم ولا تعتدوا إن الله لا يحب المعتدين} [المائدة: ٨٧].
Bukhari related it in the Book of Marriage (5075), and Muslim in the Book of Marriage (1404), both of them through Jareer, from Ismail bin Abu Khalid, from Qays (he is Ibn Abu Hazim), who said, "I heard Abdullah say" (and he narrated it). The wording is Bukhari's.