Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appears not to parse JSON #17

Closed
delwaterman opened this issue Feb 19, 2014 · 1 comment
Closed

Appears not to parse JSON #17

delwaterman opened this issue Feb 19, 2014 · 1 comment

Comments

@delwaterman
Copy link

Maybe I am doing something stupid, but it looks like the data is not even parsed:

var Client = require('node-rest-client').Client;
client = new Client();
client.get('http://localhost:5050/workbooks/TotalDailyGrossRevenue/last-refresh-time', function(data, response){
    console.log('Data is a', typeof data);
});

Results in:

$ node scripts/test_client.js
Data is a string

Curling looks fine:

$ curl -v http://localhost:5050/workbooks/TotalDailyGrossRevenue/last-refresh-time
* About to connect() to localhost port 5050 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 5050 (#0)
> GET /workbooks/TotalDailyGrossRevenue/last-refresh-time HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5
> Host: localhost:5050
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 555
< ETag: "1044588208"
< Date: Wed, 19 Feb 2014 19:40:37 GMT
< Connection: keep-alive
<
{
  "id": "TotalDailyGrossRevenue",
  "worksheets": [
    {
      "name": "2013 Goal Tracking Data"
    },
    {
      "name": "2013 Goal Tracking Graph"
    },
    {
      "name": "Best Day"
    },
    {
      "name": "By Product"
    },
    {
      "name": "Data Table"
    },
    {
      "name": "Total"
    },
    {
      "name": "2013 Revenue Goal Tracker"
    },
    {
      "name": "Data"
    },
    {
      "name": "Gross Revenue by Product"
    },
    {
      "name": "Total Gross Revenue"
    }
  ],
  "lastRefresh": "2014-02-19T18:49:45.085Z"
* Connection #0 to host localhost left intact
}* Closing connection #0
@aacerox
Copy link
Owner

aacerox commented Feb 19, 2014

Hi
by default node-rest-client has 2 content-types defined

"xmlctype":["application/xml","application/xml;charset=utf-8"],
"jsonctype":["application/json","application/json;charset=utf-8"]

note that there is no space between content-type and charset, while CURL is returning

"application/json; charset=utf-8"

with an extra space between content type and charset.

So to fix your issue you must set this content-type (with space) as a client config parameter as you can see in issue #12 and in nod-rest-client documentation.

var options ={  
    // customize mime types for json or xml connections 
    //(in your case with a space between type and charset)
    mimetypes:{
        json:["application/json","application/json; charset=utf-8"],
        xml:["application/xml","application/xml; charset=utf-8"]
    } 
};

Client client = new Client(options)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants