[SOLVED] Scripts: Debugging JSON API Call

I have…

Environment

  • [ ] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [x] Cloud version

Current behavior

I am trying to debug a script running in the creation step of a schema. The whole thing just fails around the postJSON() function. I verified every other step in the script (setting headers, creating the request body etc.) by running the script without the postJSON() call or printing the output into a field.

The whole thing just breaks on the postJSON() call. I can’t even get into the success handler. I tried just writing some static string into a field and it won’t even get into the function.

When creating an object for the corresponding schema I just get a message saying “Failed to create content. Please reload.”. The message also comes directly after trying to create the object, so it’s within the timout threshold for scripts. And I had issues with using too much compute time in the past within scripts, which resulted in an error message stating the script took to long to compute, so I suppose that also isn’t the issue.

So my issue is that I don’t get any usable feedback in order to fix whatever doesn’t work. Is there any logging capability for the script or some other way to find out, why exactly the API call doesn’t work.

I’ve verified the call itself trying it via curl and Postman. So now I’m kinda stuck.

Example

var apiUrl = "https://api.somewhere.com/v1/path"

var headers = {
    Authorization: 'Bearer superSecretToken',
    "Content-Type": 'application/json'
    
};

var requestBody = JSON.stringify({
    <some valid JSON>
  });

postJSON(apiUrl, requestBody, function(result) {
    ctx.data.textField.iv = JSON.stringify(result);
    replace();
}, headers);

Have you checked the second callback that provides the error?

A log does not exist yet, but it sounds like a good idea.

Btw: You moved from English to German.

yeah :smiley: forgot to remove the initial parts of the mail ;). (regarding the German)

What second callback? In the documentation the method signature only includes the parameters I used?

postJSON(url,body,cb,h?)

True. I thought there would be something

Btw: This is not needed:

var requestBody = JSON.stringify({
    <some valid JSON>
  });

postJSON serializes it automatically.

BTW: Can you send me a concrete example via PM?

Ah thats nice to know. PM is out :slight_smile:

1 Like

There are actually a few problems:

  1. JSON.stringify and JSON.parse is not needed. The method accepts a javascript value and returns a javascript value. Parsing and so on are done internally. Your code should probably work if you remove just that.
  2. The script engine does not expose exceptions by default, unless they are known (for security reasons). But there is a bug where exceptions in async code are not handled properly.
  3. The http extensions do not treat http-exceptions as known exceptions.

So what I will do:

  1. Fix bug #1
  2. Fix bug #2
  3. Expose an additional parameter ignoreError. When true, the callback is invoked and and error object is returned, with statusCode and response string.
1 Like

There was another problem: The API used text/json instead of application/json. Perhaps you can override it with Content-Type header, but I don’t think so.

Fixed as part of dev-7577.

1 Like

Noice, can I see somewhere on what version the cloud instance currently runs, so that I’ll know when this is live :)?

No, but I hope I can deploy it next week.

1 Like

This topic was automatically closed after 2 days. New replies are no longer allowed.