How to obtain the query string in a GET with Java HttpServer/HttpExchange?

The following: httpExchange.getRequestURI().getQuery() will return string in format similar to this: “field1=value1&field2=value2&field3=value3…” so you could simply parse string yourself, this is how function for parsing could look like: public Map<String, String> queryToMap(String query) { if(query == null) { return null; } Map<String, String> result = new HashMap<>(); for (String param : query.split(“&”)) { String[] entry … Read more

Java class for embedded HTTP server in Swing app

Since Java 6, the JDK contains a simple HTTP server implementation. Example usage: import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.Executors; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class HttpServerDemo { public static void main(String[] args) throws IOException { InetSocketAddress addr = new InetSocketAddress(8080); HttpServer server = … Read more

Running http-server in background from an npm script

You can run a process in background by appending & in the end. And then use the postscript hook that npm offers us, in order to kill the background process. “scripts”: { “web-server”: “http-server -p 7777 httpdocs &”, “pretest”: “gulp build-httpdocs && npm run web-server”, “test”: “mocha spec.js”, “posttest”: “pkill -f http-server” } But what … Read more

Get request body from node.js’s http.IncomingMessage

‘readable’ event is wrong, it incorrectly adds an extra null character to the end of the body string Processing the stream with chunks using ‘data’ event: http.createServer((r, s) => { console.log(r.method, r.url, r.headers); let body = ”; r.on(‘data’, (chunk) => { body += chunk; }); r.on(‘end’, () => { console.log(body); s.write(‘OK’); s.end(); }); }).listen(42646);

Simple HTTP server for logging requests only [closed]

For some super simple alternatives, there’s netcat: $ nc -l -p 8080 And python’s inbuilt: $ python -m SimpleHTTPServer 8080 (In recent versions of python, 3?) this is now: $ python -m http.server 8080 Netcat won’t serve responses so you may not get too far, SimpleHTTPServer won’t show POST requests (at least). But occasionally I … Read more

How to start http-server locally

When you’re running npm install in the project’s root, it installs all of the npm dependencies into the project’s node_modules directory. If you take a look at the project’s node_modules directory, you should see a directory called http-server, which holds the http-server package, and a .bin folder, which holds the executable binaries from the installed … Read more

Python HTTP Server/Client: Remote end closed connection without response error

It looks like the server is terminating the connection early without sending a full response. I’ve skimmed the docs and I think this is the problem (emphasis added): send_response(code, message=None) Adds a response header to the headers buffer and logs the accepted request. The HTTP response line is written to the internal buffer, followed by … Read more

How can I receive an uploaded file using a Golang net/http server?

Here’s a quick example func ReceiveFile(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(32 << 20) // limit your max input length! var buf bytes.Buffer // in your case file would be fileupload file, header, err := r.FormFile(“file”) if err != nil { panic(err) } defer file.Close() name := strings.Split(header.Filename, “.”) fmt.Printf(“File name %s\n”, name[0]) // Copy the … Read more