Difficulty retreiving JSON data

Nov 19, 2013 at 9:07 PM
Warning:: Rank newb here....

I'm trying to get my first RESTful (client) program working, talking to a Philips Hue (light) controller that's on my network -- it's a REST server.

Here's my very simple program (that doesn't work)...
// HueCmdLine.cpp : Defines the entry point for the console application.
//

#include <cpprest/http_client.h>
#include <cpprest/json.h>
#include <exception>

using namespace std;
using namespace web;
using namespace web::http;
using namespace web::http::client;

#ifdef _MS_WINDOWS
int wmain(int argc, wchar_t *args[])
#else
int main(int argc, char *args[])
#endif
{
    http_client client(U("http://192.168.1.113/api/newdeveloper/config"));

    std::cout << "Starting...";

    client.request(methods::GET)
    
        .then([](http_response response)
        {
            if (response.status_code() == status_codes::OK)
            {
                try {
                    auto bridgeConfig = response.extract_string().get();
                    //auto bridgeConfig = response.extract_json().get();
                    wcout << bridgeConfig << "Ending" << std::endl;
                }
                catch (exception & err) {
                    std::cout << "ERROR:" << err.what() << std::endl;
                }
            }
        })
        
        .wait();
    
    return 0;
}
My problem is, when I try to interpret the http_response, the extract_json throws an exception (Unexpected token near line 1, column 1). When I try to interpet the data with extract_string (I know I shouldn't as I see from response.header that the Content-Type is 'application/json'), I get an empty sting (but the program terminates without an exception).

I can get the same data fine from controller user the integrated web interface. The data is suppsed to look like '{ "name": "bla bla bla", "mac": <etc> }

I'm using VS2014 (Pro) with Casablanca 1.3.1
Any ideas appreciated.
Coordinator
Nov 21, 2013 at 2:14 AM
Hi Paul_Carroll,

First question when you say "VS 2014 (Pro)" do you mean Visual Studio 2013? Because there isn't a 2014 version yet :)

So the first case when you are calling extract_json it appears for some reason our parser thinks the json document is improperly formatted. Could you get the exact text of the body being send back from the server on your network using some tool like fiddler? I want to take a look at the full json document being sent, the first character for some reason is tripping up the parser. I'm wondering if their is a byte order mark or something there.

I'm also puzzled as to why extract_string is returning an empty string. Is there anyway you can share the entire http_response back to me? This could be done with fiddler or perhaps with the http_response::to_string() method, although I expect it won't properly include the body of the message based on what you describe. If you won't want to share with everyone on the forums you can send it directly to me by contacting me at stgates at Microsoft dot com.

Thanks,
Steve
Nov 22, 2013 at 12:48 AM
Hey Steve,

Thanks for the response. Yea, I did a search for VS2014 (Pro) and couldn't find one -- so I wrote one myself ;-)

Yea, I meant VS 2013.

So, back to the problem at hand, I'm afraid I didn't have much luck with fiddler. Did I mention I'm a rank newb ? LOL. Well, I did get fiddler working and it seems to be catching all of my browser type traffic, but nothing from my app. I also copied the source from the Casablancs 'BingRequest' into my project and built it (instead) -- it builds and runs fine, but again, nothing is captured by fiddler. I'm wondering if there is somethng 'funny' with my build setup, or perhaps my network settings.

Not be be deterred however, I tried cURL, and it is able to communicate fine with my REST controller. I'll e-mail you the output from it so you can at least see the json data being returned.

Thanks again.

/Paul
Coordinator
Dec 11, 2013 at 7:15 PM
Paul and I talked offline and he shared with me the response headers he was getting and I figured out what the issue is.

From your server there is no Content-Length header or Transfer-Encoding header, without out those our implementation doesn’t know how long the message body is. So when you call extract_string() the data is empty and when you call extract_json an empty string isn’t valid json so a parsing exception is thrown. This is a bug on our side according to the HTTP specification in section 4.4 Message Length the last option if no Content-Length or Transfer-Encoding is specified is by figured out the length when the server closes the connection. We will fix this bug for our next release 1.5, probably around the end of January.

Thanks Paul for taking the time to report the issue.

Steve
Jan 24, 2014 at 11:44 PM
I'm actually running into the exact same issue. The fix for this doesn't seem to have been checked in to the development branch yet. Is it likely to be checked in soon? Any more info on when we might see version 1.5?
Coordinator
Jan 27, 2014 at 5:27 PM
Hi johncuyle,

No we haven't fixed this bug yet, but do recognize its importance. Our current plan is to have it fixed in our 1.5 release, which will probably be towards the end of February.

Steve
Coordinator
Jan 27, 2014 at 5:42 PM
FYI, I've opened an issue on CodePlex to help us track this bug.

https://casablanca.codeplex.com/workitem/80
Coordinator
Apr 3, 2014 at 11:53 PM
Just to let everyone know here, this bug has been fixed in our 2.0.1 release.

Steve
Apr 4, 2014 at 12:46 AM
That's great news! Can't wait to update.