# Youseful Software - FileMaker sample script
# Script name: Youseful - Fetch JSON
# Plug-in: YsflHttp.fmx64 (Windows x64)
# Setup: Replace https://api.example.com/demo.json with a read-only JSON endpoint you control or are allowed to call. No fields needed.
# Expected: A successful 2xx response displays formatted JSON. Transport errors, non-2xx responses, and invalid JSON take separate branches.
# Format: UTF-8 Script Workspace step listing. Add the steps manually; this is not an importable FileMaker file.
# Lines beginning with # are notes; optionally add them as Comment steps.
# Preserve calculation quotes, backslashes, and paragraph symbols exactly.

Set Variable [ $url ; Value: "https://api.example.com/demo.json" ]
Set Variable [ $body ; Value: YsflHt_Get ( $url ; "timeout=15¶accept=application/json" ) ]
Set Variable [ $transportError ; Value: YsflHt_LastError ]
Set Variable [ $transportMessage ; Value: YsflHt_LastErrorText ]
Set Variable [ $status ; Value: YsflHt_Status ]
If [ $transportError <> 0 ]
    Show Custom Dialog [ "Connection failed" ; $transportMessage ]
    Exit Script [ Text Result: $body ]
End If
If [ $status < 200 or $status >= 300 ]
    Show Custom Dialog [ "HTTP " & $status ; $body ]
    Exit Script [ Text Result: $body ]
End If
Set Variable [ $formatted ; Value: JSONFormatElements ( $body ) ]
If [ Left ( $formatted ; 1 ) = "?" ]
    Show Custom Dialog [ "Response is not JSON" ; $body ]
Else
    Show Custom Dialog [ "HTTP " & $status ; $formatted ]
End If
