Getting the body of a HTTP Request
brian via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 27 15:42:54 PST 2016
Hello forumites
I am using vibe to connect to an (internal) API however, an am
expecting to get back an authorization token with the body of a
HTTP POST response.
/***** start code snippet *****/
shared static this()
{
requestHTTP("http://mywebsite.website.com/thelogonthing/oauth/authorize/login.do",
(scope req) {
req.method = HTTPMethod.POST;
req.headers["grant_type"] = "password";
req.headers["response_type"] = "token";
req.headers["client_id"] = "app";
req.headers["scope"] = "read";
req.writeJsonBody(["username":"username at blah.com"
, "password":"mySecretPassword"]);
},
(scope res) {
logInfo("Status Code: %s", res.statusCode);
logInfo("Status Phrase: %s", res.statusPhrase);
logInfo("http Version: %s", res.httpVersion);
foreach(k,v; res.headers)
logInfo("Header: %s %s", k, v);
logInfo("Content Type: %s", res.contentType);
logInfo("Body: %s", res.bodyReader());
logInfo("Body to String: %s",
res.bodyReader.readAllUTF8);
logInfo("Body to String: %s", res.toString());
logInfo("Response Length: %s", res.readJson);
string resJson = null;
}
);
...
/***** end code snippet *****/
However the response I get is empty.
The response I get for the above code is a whole lot of headers
(excellent) followed by:
/***** start log snippet *****/
...
Header: Connection Keep-Alive
Content Type: text/plain; charset=utf-8
Body: vibe.stream.counting.EndCallbackInputStream
Body to String:
Body to String: HTTP/1.1 302 Found
core.exception.AssertError at ..\..\..\..\AppData\Roaming\dub\packages\vibe-d-0.7.2
6\source\vibe\core\log.d(128): (1): Error: JSON string is empty.
Program exited with code 1
/***** end log snippet *****/
I'm a little fuzzy as to how exactly I should be parsing and then
outputting the body of the response. I've tried a few different
combinations of trying to loop through and/or convert to string
whatever is in `res.bodyReader` but honestly, I'm not sure I'm
doing it right.
Anyone able to shed some light on what the structure of the
response is, and how I can read/output it all?
Regards
Brian
More information about the Digitalmars-d-learn
mailing list