www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting the body of a HTTP Request

reply brian <brian infinityplusb.com> writes:
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 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 ..\..\..\..\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
Jan 27 2016
next sibling parent reply Chris Wright <dhasenan gmail.com> writes:
On Wed, 27 Jan 2016 23:42:54 +0000, brian wrote:
 Body: vibe.stream.counting.EndCallbackInputStream Body to String:
 Body to String: HTTP/1.1 302 Found
You got an HTTP redirect as a response. There should be a header called Location containing a URL. Redo the request with that URL. Most HTTP libraries give you an option to automatically follow redirects, but vibe.d appears not to.
Jan 27 2016
parent reply brian <brian infinityplusb.com> writes:
On Wednesday, 27 January 2016 at 23:50:34 UTC, Chris Wright wrote:
 On Wed, 27 Jan 2016 23:42:54 +0000, brian wrote:
 Body: vibe.stream.counting.EndCallbackInputStream Body to 
 String: Body to String: HTTP/1.1 302 Found
You got an HTTP redirect as a response. There should be a header called Location containing a URL. Redo the request with that URL.
Yup there was. However using that URL in place of the original just returns the html for a login page in the response. I wouldn't call myself an expert on all things API related so I'm not sure if the error is my side or not. Do API authentications usually access the same url as a normal login?
 Most HTTP libraries give you an option to automatically follow 
 redirects, but vibe.d appears not to.
Jan 27 2016
parent Chris Wright <dhasenan gmail.com> writes:
On Thu, 28 Jan 2016 00:16:12 +0000, brian wrote:

 On Wednesday, 27 January 2016 at 23:50:34 UTC, Chris Wright wrote:
 On Wed, 27 Jan 2016 23:42:54 +0000, brian wrote:
 Body: vibe.stream.counting.EndCallbackInputStream Body to String: Body
 to String: HTTP/1.1 302 Found
You got an HTTP redirect as a response. There should be a header called Location containing a URL. Redo the request with that URL.
Yup there was. However using that URL in place of the original just returns the html for a login page in the response.
Looks like you're using OAuth here. I'm not terribly familiar with it. You'll have to read the spec or look at other implementations to see what you need to implement.
Jan 27 2016
prev sibling parent yawniek <dlang srtnwz.com> writes:
On Wednesday, 27 January 2016 at 23:42:54 UTC, brian wrote:
 Anyone able to shed some light on what the structure of the 
 response is, and how I can read/output it all?

 Regards
 Brian
its unlikely that vibe client misses something. for debugging i would try to go trough all requests with curl on the commandline and see if you can make it work there. in case you are looking for an oauth client implementation there seems to be this https://github.com/danielsson/dlang-oauth2 that works with vibe.d if you add the switch for it. not sure if it works but it might be a good example.
Jan 28 2016