ApiFreeMaNDownload Desktop

05 - Sending Requests

End-to-End Flow

  1. User edits request state in renderer.
  2. Renderer builds RequestPayload.
  3. Renderer invokes desktopApi.sendRequest through preload IPC.
  4. Electron main process validates and sends network request.
  5. Main process returns response payload or structured error.

Request Payload Structure

RequestPayload includes:

  • method
  • url
  • contextFolderPath (optional)
  • headers
  • body
  • preRequestScript/testScript (optional; currently disabled by flag)

Defined in contracts model.

URL and Params Synchronization

In renderer state reducer:

  • Editing URL parses query params into editable rows.
  • Editing param rows rebuilds URL query string.

This keeps URL text and param editor in sync.

Header and Body Construction in Main Process

Main process buildRequestInit logic:

  1. Trims header keys
  2. Skips empty header keys
  3. Sends body only when method is not GET and body text is non-empty
  4. Auto-adds content-type if body exists and header not already present

Folder Variable Resolution

Before sending, main process resolves URL placeholders when contextFolderPath is provided:

  • Pattern: {{variableName}}
  • Value source: inherited folder variable files

Unresolved placeholders remain unchanged.

Network Execution

Main process performs fetch in Node context, not renderer context.

Benefits:

  • Avoids browser-level CORS restrictions in renderer
  • Centralized error capture and response normalization

Response Payload Returned to Renderer

ResponsePayload includes:

  • status
  • statusText
  • headers map
  • body text
  • durationMs
  • final URL
  • optional testResults/scriptError fields

Error Model

When request fails, SendRequestResult includes:

  • ok false
  • error message
  • optional error details
  • durationMs

Error details attempt to expose cause code/message when available.

Response Formatting in UI

Renderer formatter behavior:

  1. Checks content-type headers for json
  2. Falls back to payload shape heuristic (starts with { or [)
  3. Pretty-prints JSON with 2-space indentation
  4. Falls back to raw text when parse fails

Practical Notes

  • Invalid or empty URL triggers validation error before fetch.
  • Method/body behavior follows current implementation rules.
  • Always test requests after import because imported examples may need environment-specific tuning.