fix request being undefined

function expressions don't get hoisted.
The code was relying on the request function already being defined.
Switched to function declaration instead of reordering code.