Typical POST request I get from request builder in golang and dump request to plain. This is request example
POST /upload HTTP/1.1
Host: localhost:4500
Accept: */*
Content-Length: 191
Content-Type: multipart/form-data; boundary=------------------------c9ef9ede1067aaa2
User-Agent: curl/8.2.1
--------------------------c9ef9ede1067aaa2
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
test
--------------------------c9ef9ede1067aaa2--
The boundary
can be almost anyone html - What is the boundary in multipart/form-data? - Stack Overflow i made a random string
let boundary = rand_str(60);
let mut stream = std::net::TcpStream::connect("127.0.0.1:4500").unwrap();
stream
.write("POST /upload HTTP/1.1\r\nHost:127.0.0.1:4500\r\n".as_bytes())
.unwrap();
stream
.write(
format!(
"Content-Type: multipart/form-data;bondary={}\r\n",
&boundary
)
.as_bytes(),
)
.unwrap();
stream
.write(format!("--{}\r\n", &boundary).as_bytes())
.unwrap();
stream
.write(
"Content-Disposition: form-data; name=\"file\"; filename=\"content.dat\"\r\n"
.as_bytes(),
)
.unwrap();
stream
.write("Content-Type: application/octet-stream\r\n".as_bytes())
.unwrap();
stream.write(&data).unwrap();
let _ = match stream.write(format!("\r\n--{}--\r\n\r\n", boundary).as_bytes()) {
Ok(_) => {}
Err(err) => panic!("{}", err), //always panic: `connection reset by peer`
};
stream.flush().unwrap();
Help me, how I can make POST request with mulipart over TcpStream? Thanks for answers!