shou2017.com
JP

Rails: How to Fix 'HTTP parse error, malformed request'

Mon Apr 22, 2019
Sat Aug 10, 2024

After finishing Chapter 11 of the Rails Tutorial and partway through Chapter 12, I got the following error when running rails s:

HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.

In short, it’s telling me to access using https even in the local environment. So, I’ll modify my setup to enable https connections in the local environment.

Environment

  • macOS Mojave 10.14.4
  • Rails 5.2.2
  • ruby 2.6.1

Creating a Self-Signed Certificate

I’ll use mkcert, which makes it easy to create self-signed certificates.

Installing mkcert:

$ brew install mkcert
$ brew install nss # Also needed if you use Firefox

Creating the certificate:

$ mkcert -install
$ mkcert example.com

# Certificates are generated in the current directory
$ ls -l ./*.pem
-rw-------  1 tofu  staff  1708  4 22 20:00 ./example.com-key.pem
-rw-r--r--  1 tofu  staff  1549  4 22 20:00 ./example.com.pem

Configuring config/puma.rb

Modify config/puma.rb to reference the certificates we just created:

#  config/puma.rb

cert = "/Users/name/example.com.pem"
key = "/Users/name/example.com-key.pem"
ssl_bind "0.0.0.0", 9292, cert: cert, key: key

Finally, run pumactl start instead of rails s, and access https://localhost:9292/.

See Also