httpbin icon indicating copy to clipboard operation
httpbin copied to clipboard

Feature: env /tags endpoint and env based HTTP headers

Open simonkowallik opened this issue 6 years ago • 5 comments

Description

This feature adds a /tags endpoint and inserts HTTP headers, both based on environment variables. The new endpoint returns environment variables (and their values) starting with HTTPBIN_. XHTTPBIN_ environment variables are added as HTTP response headers. HTTPBIN_ as well as XHTTPBIN_ will be stripped from the name, additionally all underscores _ in the XHTTPBIN_ variable name will be converted to a dash - for better shell variable name and HTTP header support.

Motivation

The motivation is that load balancing/horizontal scaling tests usually require unique identification of the instance handling the request. This feature allows to uniquely tag these instances (containers or even multiple httpbin instances on the same server OS). The optional HTTP header provides this identifier across the whole httpbin feature set, not only for the /tags endpoint.

Examples

Assuming the following environment variables are set:

  • XHTTPBIN_X_TagHeader="Header Value"
  • HTTPBIN_Tag=Value
  • HTTPBIN_AnotherTag="long value..."
> curl -i http://localhost/tags
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 55
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:53:18 GMT

{
  "AnotherTag": "long value...",
  "Tag": "Value"
}

> curl -i http://localhost/tags/get/Tag
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 21
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:54:40 GMT

{
  "Tag": "Value"
}

> curl -i http://localhost/tags/get/UndefinedTag
HTTP/1.1 404 Not Found
Content-Type: application/json
Content-Length: 2
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:55:03 GMT

{}

> curl -i http://localhost/get
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 179
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:55:25 GMT

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "localhost",
    "User-Agent": "curl/7.54.0"
  },
  "origin": "172.17.0.1",
  "url": "http://localhost/get"
}

If no HTTPBIN_ environment variables exist, the response for /tags will look like this:

> curl -i http://localhost/tags
HTTP/1.1 404 Not Found
Content-Type: application/json
Content-Length: 2
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:58:07 GMT

{}

simonkowallik avatar Jul 09 '19 21:07 simonkowallik

This pull request is similar to and inspired by #552

Issue #558 and pull request #564 also show unique identification of instances is an often required feature.

simonkowallik avatar Jul 09 '19 21:07 simonkowallik

good pr, hope merged

chinaran avatar Apr 12 '21 06:04 chinaran

I just found this and would love to see it merged, as I'm playing around with multiple httpbin instances, and having a way to uniquely identify each one would be super helpful.

dmuth avatar Jul 25 '21 02:07 dmuth

@dmuth You could try https://github.com/chinaran/go-httpbin, a golang port of httpbin. I added envs param for identify httpbin instance. It will default show HOSTNAME env which in k8s is pod name.

chinaran avatar Aug 01 '21 10:08 chinaran

@dmuth You could try https://github.com/chinaran/go-httpbin, a golang port of httpbin. I added envs param for identify httpbin instance. It will default show HOSTNAME env which in k8s is pod name.

That worked for me, thanks for sharing it!

dmuth avatar Aug 08 '21 21:08 dmuth