Closed jreyesr closed 1 year ago
Steps to reproduce:
Result:
Ctrl+Alt+I
) prints a log: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined
However, other times it does work correctly when no Outputs are set
The stopping issue does appear to be related to XML in the response. Unfortunately I have to use the XML API for some calls.
Also, I figured out the scenario a bit further. When API responds with XML, if you do not set outputs then it completes and halts after 1 iteration, and does not pop the alert about incorrect content-type. As if it halts before the alert call. However, if you set an output, the iterations do not stop but the alert pops. So it seems if allowing XML responses (by testing and bypassing any JSON evaluation?) would enable iterations to continue without having to set outputs.
UPDATE: I moved around some lines in main.js to get it to bypass JSON path and it iterates.
if (outputConfig.length > 0 && response.contentType.startsWith("application/json")) {
//context.app.alert("Error!", The response has invalid Content-Type "${response.contentType}", needs "application/json"! Alternatively, delete all Outputs and try again.
);
// continue; // There's no point in attempting to parse the response, just jump to the next request
// Read the response data, then apply JSONPath expressions on it and update the CSV data
const responseData = JSON.parse((0, $d7c7cb7a1f9a8455$export$889bdc5a16325af3)(response.bodyPath));
console.debug(responseData);
for (const { name: name , jsonPath: jsonPath } of outputConfig){
let out = (0, $d7c7cb7a1f9a8455$export$7419b2935890b5eb)(jsonPath, responseData) ?? null;
console.debug(name, "+", jsonPath, "=>", out);
let nextData = [
...csvData
]; // Copy the array so that it can trigger a state update
out = JSON.stringify(out);
// BUGFIX: If value was a string, remove the quotes, since they look weird, and we expect strings to be one
// of the primary output values of the plugin
if (out.startsWith('"') && out.endsWith('"')) out = out.substring(1, out.length - 1);
nextData[i][name] = out; // Mutate the required field, save it as a string
}
// // Read the response data, then apply JSONPath expressions on it and update the CSV data
// const responseData = JSON.parse((0, $d7c7cb7a1f9a8455$export$889bdc5a16325af3)(response.bodyPath));
// console.debug(responseData);
// for (const { name: name , jsonPath: jsonPath } of outputConfig){
// let out = (0, $d7c7cb7a1f9a8455$export$7419b2935890b5eb)(jsonPath, responseData) ?? null;
// console.debug(name, "+", jsonPath, "=>", out);
// let nextData = [
// ...csvData
// ]; // Copy the array so that it can trigger a state update
// out = JSON.stringify(out);
// // BUGFIX: If value was a string, remove the quotes, since they look weird, and we expect strings to be one
// // of the primary output values of the plugin
// if (out.startsWith('"') && out.endsWith('"')) out = out.substring(1, out.length - 1);
// nextData[i][name] = out; // Mutate the required field, save it as a string
setCsvData(nextData);
}
OK. It's definitely due to XML then.
Indeed, if you do not set any outputs, the check for Content-Type JSON does not trigger (in 3), but then it goes ahead and tries to parse the body as JSON anyways (in 5), and that's killing your process. If you set at least one output, it enters (3), gives an alert but then bypasses the rest of that iteration and doesn't even try to parse it as JSON.
That should be easy to fix, by changing that logic around a bit and only making it parse the response when both a) you have requested at least one output AND b) the response returns a JSON contenttype. That will forbid extracting any information from non-JSON responses, but as I understand it you don't need it anyway, you're just calling the API for its side effects.
Reported by @dotJson in #3