factored out the http logic