Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityTestMu AI Review: How AI is Solving the Quality Engineering Problem(23.09.2026 um 13:18 Uhr)
Windows Tipps & SecurityAmazon haut den kabellosen Dyson V8 Stabstaubsauger zum Tiefstpreis raus(24.09.2026 um 09:32 Uhr)
Windows Tipps & SecurityUpdates beheben etliche Schwachstellen in Foxit PDF Reader(24.09.2026 um 09:44 Uhr)
Windows Tipps & Security„Vom Experience Center zum monumentalen Signage-Projekt“(24.09.2026 um 10:30 Uhr)
Windows Tipps & SecurityTestMu AI Review: How AI is Solving the Quality Engineering Problem(23.09.2026 um 13:18 Uhr)
Windows Tipps & SecurityAmazon haut den kabellosen Dyson V8 Stabstaubsauger zum Tiefstpreis raus(24.09.2026 um 09:32 Uhr)
Windows Tipps & SecurityUpdates beheben etliche Schwachstellen in Foxit PDF Reader(24.09.2026 um 09:44 Uhr)
Windows Tipps & Security„Vom Experience Center zum monumentalen Signage-Projekt“(24.09.2026 um 10:30 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

How to Send POST Data with fetch()

Problem You need to send POST requests with different types of data (JSON, form data, plain text) using the built-in fetch() API in Node.js, with proper headers and error handling. Solution // 1. Basic POST with JSON…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




Problem



You need to send POST requests with different types of data (JSON, form data, plain text) using the built-in fetch() API in Node.js, with proper headers and error handling.






Solution






// 1. Basic POST with JSON Data
async function postJSON(url, data) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(data)
});

if (!response.ok) {
throw new Error(`POST failed: ${response.status} ${response.statusText}`);
}

return await response.json();
} catch (error) {
console.error('POST JSON error:', error.message);
throw error;
}
}

// 2. POST with Form Data (application/x-www-form-urlencoded)
async function postFormData(url, formData) {
try {
// Convert object to URLSearchParams
const params = new URLSearchParams();
Object.keys(formData).forEach(key => {
params.append(key, formData[key]);
});

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params.toString()
});

if (!response.ok) {
throw new Error(`Form POST failed: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error('POST form data error:', error.message);
throw error;
}
}

// 3. POST with Multipart Form Data (File Uploads)
async function postMultipartForm(url, fields, files = []) {
try {
const formData = new FormData();

// Add text fields
Object.keys(fields).forEach(key => {
formData.append(key, fields[key]);
});

// Add files (in Node.js, you'd typically use file streams)
files.forEach(file => {
formData.append(file.fieldName, file.stream, file.filename);
});

const response = await fetch(url, {
method: 'POST',
body: formData
// Don't set Content-Type header - fetch will set it automatically with boundary
});

if (!response.ok) {
throw new Error(`Multipart POST failed: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error('POST multipart error:', error.message);
throw error;
}
}

// 4. POST with Authentication Headers
async function postWithAuth(url, data, token) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Accept': 'application/json',
'User-Agent': 'MyApp/1.0'
},
body: JSON.stringify(data)
});

if (response.status === 401) {
throw new Error('Authentication failed - invalid token');
}

if (!response.ok) {
throw new Error(`Authenticated POST failed: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error('Authenticated POST error:', error.message);
throw error;
}
}

// 5. POST with Custom Headers and Error Handling
async function postWithCustomHeaders(url, data, customHeaders = {}) {
try {
const defaultHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};

const response = await fetch(url, {
method: 'POST',
headers: {
...defaultHeaders,
...customHeaders
},
body: JSON.stringify(data)
});

// Handle different response types based on Content-Type
const contentType = response.headers.get('Content-Type');
let responseData;

if (contentType?.includes('application/json')) {
responseData = await response.json();
} else if (contentType?.includes('text/')) {
responseData = await response.text();
} else {
responseData = await response.text(); // Fallback to text
}

if (!response.ok) {
throw new Error(`POST failed: ${response.status} - ${responseData}`);
}

return {
status: response.status,
statusText: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
data: responseData
};
} catch (error) {
console.error('Custom headers POST error:', error.message);
throw error;
}
}

// 6. POST with Raw Text Data
async function postRawText(url, textData, contentType = 'text/plain') {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': contentType,
'Accept': 'application/json'
},
body: textData
});

if (!response.ok) {
throw new Error(`Text POST failed: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error('POST raw text error:', error.message);
throw error;
}
}

// 7. POST with Binary Data
async function postBinaryData(url, binaryData) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'Content-Length': binaryData.length.toString()
},
body: binaryData
});

if (!response.ok) {
throw new Error(`Binary POST failed: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error('POST binary data error:', error.message);
throw error;
}
}

// 8. POST with Timeout and Retry
async function postWithRetry(url, data, options = {}) {
const {
maxRetries = 3,
timeoutMs = 5000,
retryDelay = 1000
} = options;

let lastError;

for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
console.log(`POST attempt ${attempt}/${maxRetries}`);

const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(data),
signal: controller.signal
});

clearTimeout(timeoutId);

if (!response.ok) {
throw new Error(`POST failed: ${response.status}`);
}

return await response.json();
} catch (error) {
lastError = error;

if (error.name === 'AbortError') {
console.log(`Attempt ${attempt} timed out after ${timeoutMs}ms`);
} else {
console.log(`Attempt ${attempt} failed:`, error.message);
}

if (attempt === maxRetries) {
break;
}

// Wait before retry
await new Promise(resolve => setTimeout(resolve, retryDelay * attempt));
}
}

throw lastError;
}

// 9. Batch POST Requests
async function batchPOST(requests) {
try {
const promises = requests.map(async (req, index) => {
try {
const response = await fetch(req.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...req.headers
},
body: JSON.stringify(req.data)
});

if (!response.ok) {
throw new Error(`Request ${index} failed: ${response.status}`);
}

return {
index,
success: true,
data: await response.json()
};
} catch (error) {
return {
index,
success: false,
error: error.message
};
}
});

const results = await Promise.allSettled(promises);

return results.map(result => result.value);
} catch (error) {
console.error('Batch POST error:', error.message);
throw error;
}
}

// 10. POST with Progress Tracking (for large uploads)
async function postWithProgress(url, data, onProgress) {
try {
const jsonData = JSON.stringify(data);
const totalSize = new Blob([jsonData]).size;
let uploadedSize = 0;

// For real progress tracking with large files, you'd need streams
// This is a simplified example
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': totalSize.toString()
},
body: jsonData
});

if (onProgress) {
onProgress(100); // Report 100% completion
}

if (!response.ok) {
throw new Error(`POST with progress failed: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error('POST with progress error:', error.message);
throw error;
}
}

// Usage Examples
async function runPostExamples() {
console.log('=== POST Data Examples ===');

try {
// 1. JSON POST
console.log('1. JSON POST...');
const jsonResult = await postJSON('https://httpbin.org/post', {
name: 'John Doe',
email: '[email protected]',
age: 30
});
console.log('JSON POST success:', jsonResult.json);

// 2. Form data POST
console.log('2. Form data POST...');
const formResult = await postFormData('https://httpbin.org/post', {
username: 'johndoe',
password: 'secret123',
remember: 'true'
});
console.log('Form POST success:', formResult.form);

// 3. POST with authentication
console.log('3. Authenticated POST...');
const authResult = await postWithAuth(
'https://httpbin.org/post',
{ message: 'Hello from authenticated user' },
'your-jwt-token-here'
);
console.log('Auth POST success:', authResult.json);

// 4. POST with custom headers
console.log('4. Custom headers POST...');
const customResult = await postWithCustomHeaders(
'https://httpbin.org/post',
{ data: 'custom headers example' },
{
'X-Custom-Header': 'MyValue',
'X-API-Version': '1.0'
}
);
console.log('Custom headers POST success');

// 5. POST with retry
console.log('5. POST with retry...');
const retryResult = await postWithRetry(
'https://httpbin.org/post',
{ message: 'retry example' },
{ maxRetries: 2, timeoutMs: 3000 }
);
console.log('Retry POST success:', retryResult.json);

} catch (error) {
console.error('Example error:', error.message);
}
}

// Run examples if this file is executed directly
if (require.main === module) {
runPostExamples();
}

module.exports = {
postJSON,
postFormData,
postMultipartForm,
postWithAuth,
postWithCustomHeaders,
postRawText,
postBinaryData,
postWithRetry,
batchPOST,
postWithProgress
};









Explanation



POST requests with fetch() require setting the method: 'POST' and including data in the body property. For JSON data, stringify the object and set Content-Type: application/json. For form data, use URLSearchParams or FormData objects.



Always check response.ok before processing the response, as fetch() doesn't throw errors for HTTP error status codes. Set appropriate headers like Authorization for authenticated requests and handle different response content types based on the Content-Type header.

SOC Incident Playbook: Vulnerability Remediation & Verification
title: Detect Exploitation - How to Send POST Data with fetch()
id: b42b4453-dc7c-48e3-9c0f-169ea7792d2c
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "How to Send POST Data with fet" ascii wide
    condition:
        any of them
}
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich How to Send POST Data with fetch().... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Send POST Data with fetch()

Thematisch verwandte Begriffe: Send, POST, Data, with · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-97056 | SigNoz versions from v0.98.0 up to (but not including) v0.143.0, when co…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick