Could not parse JSON from Code's result

(Vũ Tuấn Anh) #1

I am using the following Javascript code to get the Google Search Result:

var url = inputs.url;
var opts = {
method: ‘GET’,
url: url
};

utils.request(opts, function(err, resp, result) {
if(err) {
return reject(err);
}
resolve(result);
});

It seems good and I can be received JSON as the result.

Next, I create the new action (Bot - Send a message) to print out some part of JSON result.
My message using the following block:
{{code_execute_code.result__kind}}

=> In the logs: I received the error: “Message can’t be blank”

If i using {{code_execute_code.result}} => It will print out the full of JSON result.

My logs:

My flow:

Thanks in advanced

(Kellsey Shaw) #2

Hi there,

I think the problem might be with the response format. :thinking:

Could you do me a favor and copy the full response so that I can check it’s valid JSON that our Data Outputs service can work with?

Thanks

(Sebastian Weiß) #3

Got the same problem here.

I run the following code

var url = inputs.url;
var user = inputs.user;
var pass = inputs.pass;
var opts = {
  method: 'GET',
  url: url,
  auth: {
        'user': user,
        'pass': pass,
	'sendImmediately': false
    },

};

utils.request(opts, function(err, resp, body) {
  if(err) { 
    return reject(err);
  }
  resolve(body);
});

When i output the body with
{{myapi.result}}
it returns
{"data":{"version":"5.2.22","revision":"201704210836"},"success":true}

When I try to use
{{mycode.result__data__version}}

It outputs nothing.

How can I output specific data from my JSON?

(John Jackson) #4

Would you be able to screenshot the output from the code task in the log?

You might have to try:

resolve(JSON.parse(body));

Although you shouldn’t need to do this. Or, I’m sure you could do this using the webhook/HTTP service, that would be a better way of making a HTTP request unless you need to do other things around it?

1 Like
(Sebastian Weiß) #5

THANKS! That solved the problem.

I really would like to do it with the webhook service. But I require Digest Auth for Authentificate with my library.

To enable Digest Auth I need to set
'sendImmediately': false
Liked described here: https://github.com/request/request#http-authentication

I made a feature request by email to get that implemente in the Webhook/HTTP service

with JSON.parse:

without JSON.parse

(Tom Spencer) #6

Glad you got this resolved. For future reference, it’s better to use the json: true option when sending the request. This automatically sets the correct request headers to ask for JSON content, and runs JSON.parse() on the response.

So your request options would look like:

var opts = {
  method: 'GET',
  url: url,
  json: true,
  auth: {
        'user': user,
        'pass': pass,
	'sendImmediately': false
    },

};
2 Likes