1

I am making a simple http request to the Sphere Engine API with some request parameters . However, I cannot interpret the error . API specification : http://sphere-engine.com/services/docs/compilers#status

Code:

http = require('http') ;

var info = {
    sourceCode: 'print 3+4',
    language: 4,
    input: ''
} ;

var infoString = JSON.stringify(info);

var options = {
    host: 'api.compilers.sphere-engine.com',
    port: 80,
    path: '/api/v3/submissions?access_token=b11bf50b8a391d4e8560e97fd9d63460',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': infoString.length
    }
} ;

var req = http.request(options,function(res) {
    res.setEncoding('utf-8');
    var responseString = '' ;
    res.on('data', function(data) {
        responseString += data ;
    });
    res.on('end', function() {
        var resultObject = JSON.parse(responseString);
    });
} );

req.write(infoString);
req.end();

Error:

undefined:0

^
SyntaxError: Unexpected end of input
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/Users/sarthakmunshi/nodetry.js:29:27)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)
4
  • The error simply says that the responseString is HTML instead of JSON. I have a strong suspicion that it's the login page. You can get a better clue by printing out the responseString before you try to JSON.parse() it. Commented Jun 3, 2015 at 8:36
  • I had a doubt. Is the method mentioned above similar to making a curl request with some parameters. if not, how can i make it that way ? Commented Jun 3, 2015 at 8:39
  • Inside the res.on('end'..) simply print out (console.log) the responseString and comment out JSON.parse(). Commented Jun 3, 2015 at 8:41
  • i get a json object {"error":"WRONG_LANG_ID"} . n0ow is this an api error i guess !? Commented Jun 3, 2015 at 8:48

1 Answer 1

2

This error caused by JSON.parse(responseString);. You get response as not-json string (XML, HTML?), but try to parse it as a json.

You can use xml-stream library to parse XML.

Sign up to request clarification or add additional context in comments.

4 Comments

how do i use it for converting xml to json . documentation is a bit confusing .
The response is not XML. It's HTML. Specifically HTML 2.0, not even XHTML. Parsing HTML as XML will result in errors because HTML have tags that breaks XML rules
@slebetman probably you right, I can't see the API specification because it's behind the login page. Obviously it's not a JSON.
@saru95 it's really easy. Just create xmlStream object, define callbacks for nodes/attributes you're interested in and pipe http response stream into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.