liveBook · Manning

HTTP PUT method

As explained in Building User-Friendly DSLs

An HTTP request method that replaces or creates the representation of a resource with request-provided content.

HTTP PUT saves a complete new representation for a resource. A client sends the replacement content in the request body; the server stores it and makes that content the resource’s current value. For a single persisted AST, PUT /contents overwrites the JSON file backing that resource. section 7.1 section 7.5

A Save action can serialize the edited AST, stringify it as JSON, and send it with fetch. Set method to PUT and Content-Type to application/json so the server can parse the request body. section 7.5

Saving edited DSL contents

The client sends serialize(state.ast) as JSON to /contents. Express installs express.json with a 1gb limit, reads request.body, writes it to storage, updates the in-memory contents value, and returns an empty response. Reloading the app then retrieves the saved representation with GET. section 7.5 section 7.1

Listing 7.28 Server code to handle PUT requests
const { readContents, writeContents } = 
     require("./storage")          #1

// ...other code... 

server.use(express.json({ limit: "1gb" }))        #2
server.put("/contents", (request, response) => { 
    const newContents = request.body 
    writeContents(newContents) 
    contents = newContents 
    response.send() 
})

This handler shows the complete server-side path from JSON body parsing through file storage and in-memory replacement. section 7.5

How Building User-Friendly DSLs covers it

How deep it goes
Implements client serialization and an Express handler, while deferring reliability and performance testing to later work.
Examples
Uses a Save button that sends a serialized Rental AST to /contents and verifies persistence by reload or file inspection.
What it assumes
Assumes a frontend editor, a persisted AST, and basic JSON storage before adding saving.
What you can do after
Apply it via the Fetch API and Express to save a JSON-serialized AST resource.
How it connects
Connects PUT to HTTP GET, REST APIs, the Fetch API, JSON serialization, Express, and AST persistence.

Also explained in

Compare how 8 books explain HTTP PUT method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.