Ensure we write json header before writing bytes

In net/http once we've issued a Write() the response is sent
over the wire including the header! The tests didn't catch
this because I used a net/http/httptest.RequestRecorder
which doesn't follow those semantics.
Merge pull request #46 from jpfuentes2/jf_fix_json_header

agent: Ensure we write json header before writing bytes