Sign inDemoInstall

Package Overview
File Explorer

Install Socket

Protect your apps from supply chain attacks


Package httpbin providers HTTP handlers for endpoints and a multiplexer to directly hook it up to any http.Server or httptest.Server.


Version published


# go-httpbin

[![Read GoDoc](](
[![Build Status](](

A Go handler that lets you test your HTTP client, retry logic, streaming behavior, timeouts etc.
with the endpoints of [][ht] locally in a [`net/http/httptest.Server`][hts].

This way, you can write tests without relying on an external dependency like [][ht].

## Endpoints

- `/ip` Returns Origin IP.
- `/user-agent` Returns user-agent.
- `/headers` Returns headers.
- `/get` Returns GET data.
- `/status/:code` Returns given HTTP Status code.
- `/redirect/:n` 302 Redirects _n_ times.
- `/absolute-redirect/:n` 302 Absolute redirects _n_ times.
- `/redirect-to?url=foo` 302 Redirects to the _foo_ URL.
- `/stream/:n` Streams _n_ lines of JSON objects.
- `/delay/:n` Delays responding for _min(n, 10)_ seconds.
- `/bytes/:n` Generates _n_ random bytes of binary data, accepts optional _seed_ integer parameter.
- `/cookies` Returns the cookies.
- `/cookies/set?name=value` Sets one or more simple cookies.
- `/cookies/delete?name` Deletes one or more simple cookies.
- `/drip?numbytes=n&duration=s&delay=s&code=code` Drips data over a duration after
  an optional initial _delay_, then optionally returns with the given status _code_.
- `/cache` Returns 200 unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304.
- `/cache/:n` Sets a Cache-Control header for _n_ seconds.
- `/gzip` Returns gzip-encoded data.
- `/deflate` Returns deflate-encoded data.
- `/brotli` Returns brotli-encoded data.
- `/robots.txt` Returns some robots.txt rules.
- `/deny` Denied by robots.txt file.
- `/basic-auth/:user/:passwd` Challenges HTTP Basic Auth.
- `/hidden-basic-auth/:user/:passwd` Challenges HTTP Basic Auth and returns 404 on failure.
- `/html` Returns some HTML.
- `/xml` Returns some XML.
- `/image/gif` Returns page containing an animated GIF image.
- `/image/png` Returns page containing a PNG image.
- `/image/jpeg` Returns page containing a JPEG image.

## How to use

Standing up a Go server running httpbin endpoints is just 1 line:

package main

import (

func main() {
	log.Fatal(http.ListenAndServe(":8080", httpbin.GetMux()))

Let's say you do not want a server running all the time because you just want to
test your HTTP logic after all. Integrating `httpbin` to your tests is very simple:

package test

import (


func TestDownload(t *testing.T) {
    srv := httptest.NewServer(httpbin.GetMux())
    defer srv.Close()

    resp, err := http.Get(srv.URL + "/bytes/65536")
    if err != nil {
    // read from an actual HTTP server hosted locally
    // test whatever you are going to test...

go-httpbin works from the command line as well:

$ go get
$ $GOPATH/bin/httpbin -host :8080

# Development

You must have the following tools installed on your system:

- [Glide]( 0.12.0 or above
- [Go]( 1.7 or above

To get started, simply run `glide install` to install all the dependencies of this package.
Then, run `go test $(glide nv)` to test it.

# License

Copyright 2016 Ahmet Alp Balkan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

# Authors

- Ahmet Alp Balkan ([@ahmetb][tw])



Last updated on 21 Sep 2020

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc