"Life is all about sharing. If we are good at something, pass it on." - Mary Berry

How to send an HTTP request without using curl?

2022-05-22

Categories: Networking

Problem

We are using JWT validation. For some reasons, when testing on staging, we got 401 error:

1[GIN] 2022/05/20 - 14:20:57 | 401 |    2.588128ms |       127.0.0.1 | POST     "/v1/endpoint"

Troubleshooting

After looking at the source code, we need to set the operation_debug to true to see what caused that error:

12022/05/20 08:31:26 KRAKEND ERROR: [ENDPOINT: /v1/endpoint][JWTValidator] Unable to validate the token: should have a JSON content type for JWKS endpoint

The thing is when testing locally or use port forwarding we see that the Content-Type is set correctly:

1$ http get localhost:50050/.well-known/jwks.json
2HTTP/1.1 200 OK
3Content-Length: 419
4Content-Type: application/json
5Date: Fri, 20 May 2022 08:54:56 GMT

So, what could be the reason?

After ssh to that pod, I realized that curl, wget, nc, … is not installed. And I don’t have permission to do that. I was wondering if I can send an HTTP request without using any external tool?

Solution

Reading Redirections section in the Bash manual, I saw this:

/dev/tcp/host/port

If host is a valid hostname or Internet address, and port is an integer port number or service name, Bash attempts to open the corresponding TCP socket.

So, looks like I can use this Bash’s feature to open a TCP socket to the target host.

First, we need to open a file descriptor for reading and writing on the specified TCP socket:

1$ exec 3<>/dev/tcp/s-auth/50050

Then send an HTTP request to that socket:

1$ echo -e "GET /.well-known/jwks.json HTTP/1.1\nHost: s-auth\nConnection: close\n\n" >&3

You can see this request header by using curl:

1$ curl -v localhost:50050/.well-known/jwks.json
2*   Trying ::1:50050...
3* Connected to localhost (::1) port 50050 (#0)
4> GET /.well-known/jwks.json HTTP/1.1
5> Host: localhost:50050
6> User-Agent: curl/7.77.0
7> Accept: */*

And read the response:

1$ cat <&3
2HTTP/1.1 502 Bad Gateway
3content-length: 87
4content-type: text/plain
5date: Fri, 20 May 2022 09:33:33 GMT
6server: envoy
7x-envoy-upstream-service-time: 13
8
9upstream connect error or disconnect/reset before headers. reset reason: protocol error

I will write another blog post to troubleshoot this error.

Tags: bash http

Edit on GitHub

Related Posts: