0

Using the method described in this answer (clojure.java.io/input-stream), how would I go about setting the User-Agent request header?

Mark C
  • 739
  • 1
  • 7
  • 16

2 Answers2

2

You can't. clojure.java.io is a very simplistic API, for doing the easiest stuff. If you want any real customization you'll need to use a real HTTP library.

amalloy
  • 89,153
  • 8
  • 140
  • 205
1

You can, but only to a certain extent, setting a property.

From this answer https://stackoverflow.com/a/47300260/483566 I tried running netcat in a terminal: nc -l -p 8080

On the REPL, I tried the following:

$ lein repl
nREPL server started on port 42819 on host 127.0.0.1 - nrepl://127.0.0.1:42819
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
OpenJDK 64-Bit Server VM 11.0.4+11-post-Ubuntu-1ubuntu218.04.3
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

user=> (System/setProperty "http.agent" "Clojure REPL")
nil
user=> (slurp "http://localhost:8080/")

Netcat is not actually serving content, so the REPL will block, but if you see the terminal where netcat runs, you'll see something like:

GET / HTTP/1.1
User-Agent: Clojure REPL Java/11.0.4
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

For actual customization, you probably want to use an HTTP client library that provides access to the HTTP headers (most will do).

Denis Fuenzalida
  • 3,271
  • 1
  • 17
  • 22
  • Thanks! I'm looking at `clj-http`. I'm coming from Python and used `requests` almost exclusively and this looks somewhat equivalent. – Mark C Sep 26 '19 at 11:35
  • 1
    That's a good library. If you are looking for a curated list of libraries, you can look at https://www.clojure-toolbox.com/ – Denis Fuenzalida Sep 27 '19 at 02:26