# Getting http stub to work

**URL:** https://clojureverse.org/t/getting-http-stub-to-work/6795
**Category:** Troubleshooting
**Tags:** clojure
**Created:** [November 6, 2020, 3:18am UTC](https://clojureverse.org/t/getting-http-stub-to-work/6795 "2020-11-06T03:18:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ashwinbhaskar](https://clojureverse.org/user_avatar/clojureverse.org/ashwinbhaskar/32/2045_2.png) [@ashwinbhaskar](https://clojureverse.org/u/ashwinbhaskar)
#### Post date: [November 6, 2020, 3:18am UTC](https://clojureverse.org/t/getting-http-stub-to-work/6795/1 "2020-11-06T03:18:59Z")

</div>

I am using [this](https://github.com/johanhaleby/stub-http) library to stub http requests. I just followed the instructions from read me. But I get connection refused error

```auto
Execution error (ConnectException) at java.net.PlainSocketImpl/socketConnect (PlainSocketImpl.java:-2).
Connection refused (Connection refused)

```

This is the code. What am I doing wrong?

```auto
(stub/with-routes!
  {"/foo" {:status 200
           :content-type "application/json"
           :body "{\"code\":\"SUCCESS\"}"}}
  (http/get "http://localhost/foo/"))

```

**EDIT**  
Dug a bit deeper to check see the default port which turns out to be `65468`. But still the same response

---

<div class="post-metadata">

### Author: ![Phill](https://clojureverse.org/letter_avatar_proxy/v4/letter/p/779978/32.png) [@Phill](https://clojureverse.org/u/Phill)
#### Post date: [November 6, 2020, 10:52am UTC](https://clojureverse.org/t/getting-http-stub-to-work/6795/2 "2020-11-06T10:52:05Z")

</div>

Did the readme’s examples work? It is a bit weird that the code here stubs `/foo` and then requests `/foo/`.

On the theory that you post here because you are interested in the opinions of generalists, I would venture to suggest (and the readme also hints of this) that the circumstances are unfortunate, when you need to intercept your program’s outgoing HTTP requests and divert them to a webserver your program runs on a random ephemeral port. Wrong way. Red blinky lights.

---

<div class="post-metadata">

### Author: ![xfthhxk](https://clojureverse.org/user_avatar/clojureverse.org/xfthhxk/32/570_2.png) [@xfthhxk](https://clojureverse.org/u/xfthhxk)
#### Post date: [November 7, 2020, 12:35pm UTC](https://clojureverse.org/t/getting-http-stub-to-work/6795/3 "2020-11-07T12:35:57Z")

</div>

Hi. Not sure what the context is here. If you are trying to do this in a test, have you considered using `with-redefs`? Especially if you can isolate the network call in a function by itself ie

```auto
(ns xyz
  (:require [clj-http.client :as http])
  
(defn get-foo []
  (http/get "http://api.example.com/foo"))

```

and then in your test or elsewhere

```auto
(ns xyz-test
  (:require [xyz])

(with-redefs [xyz/get-foo (constantly {:status 200
                                       :content-type "application/json"
                                       :body "{\"code\":\"SUCCESS\"}"})]
   (xyz/get-foo))
```

---

<div class="post-metadata">

### Author: ![Jeiwan](https://clojureverse.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@Jeiwan](https://clojureverse.org/u/Jeiwan)
#### Post date: [November 8, 2020, 12:26pm UTC](https://clojureverse.org/t/getting-http-stub-to-work/6795/4 "2020-11-08T12:26:00Z")

</div>

You need to use `uri` from `stub`. `stub` starts a local server on a random port and you need to send requests to that server. Take a look at how they send a request in this example:

> **[johanhaleby/stub-http](https://github.com/johanhaleby/stub-http#example-1---simple-macro)**
>
> Client library agnostic way to stub HTTP endpoints in Clojure - johanhaleby/stub-http

---

<div class="post-metadata">

### Author: ![ashwinbhaskar](https://clojureverse.org/user_avatar/clojureverse.org/ashwinbhaskar/32/2045_2.png) [@ashwinbhaskar](https://clojureverse.org/u/ashwinbhaskar)
#### Post date: [November 8, 2020, 12:55pm UTC](https://clojureverse.org/t/getting-http-stub-to-work/6795/5 "2020-11-08T12:55:57Z")

</div>

But then [this](https://github.com/johanhaleby/stub-http#example-1---simple-macro) does not use server at all.

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [May 10, 2021, 12:56am UTC](https://clojureverse.org/t/getting-http-stub-to-work/6795/6 "2021-05-10T00:56:01Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
