Can u please help me, how to get magic code before putting sending the image to the browser on server side?
This one is easy, you can't. The response (the magic code) is private and not accessible. You should use the validate method.
For your information, there is a user mailing list to ask this kind of questions.