Search code examples
urlclojuredownloadio

How can I set the User-Agent header when downloading a file in Clojure?


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


Solution

  • 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).