Request objects and JSON submission
Quick answer
Request describes a fetch operation without sending it. Set method, headers, body and signal according to the endpoint contract.
How it works
Use new Request(url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(data)}). Fetch does not automatically serialize ordinary objects as JSON. GET and HEAD requests cannot have a non-null body. Request construction alone sends nothing; the example inspects the request locally.
Local examples
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {const r=new Request("https://example.test/items",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({qty:3})});console.log(r.method,r.headers.get("content-type"),await r.text());})()Expected console output:
POST application/json {"qty":3}The request holds serialized JSON.
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {try{new Request("https://example.test/items",{method:"GET",body:"x"});}catch(e){console.log(e.name);}})()Expected console output:
TypeErrorGET with a body fails construction.
Common mistakes
Do not put private credentials in URLs. Content-Type describes bytes; it does not validate or serialize data.
Guided practice
Construct a POST request with a JSON record containing an item identifier and quantity. Inspect method and body without sending it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Request objects and JSON submission — Knowledge Check
Apply the concept and reason about the example output.
Does constructing a Request transmit it?
What converts a plain object into JSON request text?
Where should a secret token not be placed?
What is the expected console output of Request objects and JSON submission example 1?
(async () => {const r=new Request("https://example.test/items",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({qty:3})});console.log(r.method,r.headers.get("content-type"),await r.text());})()What is the expected console output of Request objects and JSON submission example 2?
(async () => {try{new Request("https://example.test/items",{method:"GET",body:"x"});}catch(e){console.log(e.name);}})()