Running httpbin on CloudRun
httpbin is a developer utility web application that receives a request and spits out the request data as a response. httpbin is for testing your API requests, webhook requests, etc.
I usually use it when developing client-side applications to watch what I am sending and what the server is receiving. For example, if I am making a simple curl POST to the remote server and if I want to know exactly what the server is receiving, then I can do
curl -X POST "https://httpbin.org/post" -H "accept: application/json"
and it returns
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
"X-Amzn-Trace-Id": "Root=1-6115338b-5a9fad6351af744531493045"
},
"json": null,
"origin": "49.207.207.139",
"url": "https://httpbin.org/post"
}
httpbin is very useful if you try to see what kind of headers or payload your application sends. You might be able to debug better. You could use the public instance at https://httpbin.org.
I know that, at least according to the code, httpbin doesn't do anything scary with your data posted. But if you want to be extra careful, you can build and run your instance.
Recently I did install it on Google's CloudRun so that I could use it for my personal use. There are two ways to do it. You can build a Docker image using the Dockerfile provided by the project. Just one thing make sure you expose $PORT
instead of 80
. And start the gunicorn command on $PORT
. This is required because by default CloudRun listens on port 8080
or the value defined by $PORT
.
The container must listen for requests on 0.0.0.0 on the port to which requests are sent. By default, requests are sent to 8080, but you can configure Cloud Run to send requests to the port of your choice. Cloud Run injects the PORT environment variable into the container. Inside Cloud Run container instances, the value of the PORT environment variable always reflects the port to which requests are sent. It defaults to 8080.
Listening for requests on the correct port
If you plan to use the existing Docker image, then you can deploy using the command.
gcloud run deploy my-httpbin --allow-unauthenticated --platform managed --image mirror.gcr.io/kennethreitz/httpbin
This command will deploy the public image from the DockerHub. But after deployment, it fails to start. Because CloudRun is exposing only 8080. To fix this issue, log into your Google Cloud Console. Go to CloudRun and your httpbin instance. Edit the container general details. Set its value to 80
. This change should start the service.
Now you have your own instance of httpbin. Of course, it's open to the world once people get to know the URL. That is because if we did --allow-unauthenticated
as part of our deployment. You can play with a different type of authentication if required.
Hey! I’m getting “upstream connect error or disconnect/reset before headers. reset reason: protocol error” when I try to use curl to post. I’m a novice at this, and followed the instructions.