Form Data
@t-req/core provides a simple syntax for form submissions, automatically choosing between multipart/form-data and application/x-www-form-urlencoded.
URL-Encoded Forms
Section titled “URL-Encoded Forms”For simple forms without files:
POST https://api.example.com/login
username = {{user}}password = {{pass}}This sends:
Content-Type: application/x-www-form-urlencoded
username=john&password=secretMultipart Forms
Section titled “Multipart Forms”When files are included, @t-req/core uses multipart:
POST https://api.example.com/profile
name = John Doeavatar = @./images/photo.jpgThis sends:
Content-Type: multipart/form-data; boundary=...Form Syntax
Section titled “Form Syntax”Text Fields
Section titled “Text Fields”field = valuefield=valuefield = value with spacesSpaces around = are optional. Values are trimmed.
File Fields
Section titled “File Fields”file = @./path/to/file.pdffile = @./path/to/file.pdf | custom-name.pdfVariables in Fields
Section titled “Variables in Fields”POST https://api.example.com/contact
name = {{userName}}email = {{userEmail}}message = {{userMessage}}attachment = @./{{attachmentPath}}Form Detection Rules
Section titled “Form Detection Rules”@t-req/core determines whether a body should be parsed as form data based on these rules:
- All non-empty lines must match the
name = valuepattern - Single-line body with
&is treated as pre-encoded URL query string, not form data - Explicit non-form Content-Type (like
application/jsonortext/plain) bypasses form parsing
# This is parsed as form dataPOST https://api.example.com/submit
name = Johnemail = john@example.com# This is NOT parsed as form data (single line with &)POST https://api.example.com/submit
name=John&email=john@example.com# This is NOT parsed as form data (explicit Content-Type)POST https://api.example.com/submitContent-Type: application/json
name = Johnemail = john@example.comContent-Type Inference
Section titled “Content-Type Inference”@t-req/core automatically sets the Content-Type:
| Condition | Content-Type |
|---|---|
| Only text fields | application/x-www-form-urlencoded |
| Any file fields | multipart/form-data |
Explicit Content-Type
Section titled “Explicit Content-Type”Override the automatic detection if needed:
POST https://api.example.com/dataContent-Type: application/x-www-form-urlencoded
field1 = value1field2 = value2JSON Body Alternative
Section titled “JSON Body Alternative”For APIs expecting JSON, use a regular body:
POST https://api.example.com/loginContent-Type: application/json
{ "username": "{{user}}", "password": "{{pass}}"}Special Characters
Section titled “Special Characters”Form values are automatically URL-encoded:
POST https://api.example.com/search
query = hello world & morespecial = symbols!@#$%Becomes: query=hello%20world%20%26%20more&special=symbols%21%40%23%24%25
Array Fields
Section titled “Array Fields”Some APIs expect array fields:
POST https://api.example.com/settings
tags = javascripttags = typescripttags = nodejsThis sends three separate tags fields, which many frameworks interpret as an array.
Empty Values
Section titled “Empty Values”Empty values are valid:
POST https://api.example.com/form
required_field = some valueoptional_field =Combining with Headers
Section titled “Combining with Headers”Add custom headers alongside form data:
POST https://api.example.com/uploadAuthorization: Bearer {{token}}X-Request-ID: {{$uuid()}}
title = My Documentfile = @./document.pdfDebugging Form Submissions
Section titled “Debugging Form Submissions”To see what’s being sent, use the engine with event logging:
import { createEngine } from '@t-req/core';import { createFetchTransport } from '@t-req/core/runtime';
const engine = createEngine({ transport: createFetchTransport(fetch), onEvent: (e) => { if (e.type === 'request:start') { console.log('Headers:', e.request.headers); console.log('Body:', e.request.body); } },});