One problem was that it was only writing the body, not headers. Still not working properly though :(