fix: endpoint test button handles binary (xlsx/pdf) responses correctly
Previously json_encode failed on binary bytes → empty body → JS crash. Now binary responses show "Archivo recibido: xlsx — 8.7 KB" instead. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
e177919be2
commit
e6dbd5c5bf
@@ -2007,13 +2007,22 @@ HTML;
|
||||
|
||||
if ($err) { echo json_encode(['ok' => false, 'error' => 'cURL: ' . $err]); exit; }
|
||||
|
||||
$decoded = json_decode($resp, true);
|
||||
$preview = is_array($decoded) ? $decoded : (string)$resp;
|
||||
$isBinary = !empty($contentType) && !str_contains($contentType, 'json') && !str_contains($contentType, 'text');
|
||||
$decoded = !$isBinary ? json_decode($resp, true) : null;
|
||||
|
||||
if ($isBinary) {
|
||||
$preview = sprintf('Archivo recibido: %s — %s KB', explode(';', $contentType)[0], round(strlen($resp) / 1024, 1));
|
||||
} elseif (is_array($decoded)) {
|
||||
$preview = $decoded;
|
||||
} else {
|
||||
$preview = mb_convert_encoding(substr((string)$resp, 0, 500), 'UTF-8', 'UTF-8');
|
||||
}
|
||||
|
||||
// Guardar última respuesta
|
||||
if ($key !== '') {
|
||||
$logVal = $isBinary ? "binary:{$contentType}:" . strlen($resp) . 'B' : substr((string)$resp, 0, 2000);
|
||||
db()->prepare("UPDATE company_endpoints SET last_response=?, last_called_at=NOW() WHERE company_id=? AND endpoint_key=?")
|
||||
->execute([is_string($resp) ? substr($resp, 0, 2000) : '', $companyId, $key]);
|
||||
->execute([$logVal, $companyId, $key]);
|
||||
}
|
||||
|
||||
if ($httpCode >= 200 && $httpCode < 300) {
|
||||
|
||||
Reference in New Issue
Block a user