example.scm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. (use-modules (web client)
  2. #;(ice-9 receive))
  3. (receive (response response-text)
  4. (http-get "http://duckduckgo.com"
  5. #:port (open-socket-for-uri "http://duckduckgo.com")
  6. #:version '(1 . 1)
  7. #:keep-alive? #f
  8. #:headers '()
  9. #:decode-body? #t
  10. #:streaming? #f)
  11. (display response)
  12. (display response-text))
  13. (call-with-values
  14. (lambda ()
  15. (http-get "https://duckduckgo.com"
  16. #:port (open-socket-for-uri "https://duckduckgo.com")
  17. #:version '(1 . 1)
  18. #:keep-alive? #f
  19. #:headers '()
  20. #:decode-body? #t
  21. #:streaming? #f))
  22. (lambda (response response-text)
  23. (display response)
  24. (display response-text)
  25. (display (response-headers response))))
  26. #|
  27. HEADERS:
  28. '((server . nginx)
  29. (date . #<date nanosecond: 0 second: 32 minute: 30 hour: 13 day: 2 month: 6 year: 2019 zone-offset: 0>)
  30. (content-type text/html (charset . UTF-8))
  31. (content-length . 5418)
  32. (connection close)
  33. (vary accept-encoding)
  34. (etag 5cf2d4ab-152a . #t)
  35. (strict-transport-security . max-age=31536000)
  36. (x-frame-options . SAMEORIGIN)
  37. (content-security-policy . default-src https: blob: data: 'unsafe-inline' 'unsafe-eval'; frame-ancestors 'self')
  38. (x-xss-protection . 1;mode=block)
  39. (x-content-type-options . nosniff)
  40. (referrer-policy . origin)
  41. (expect-ct . max-age=0)
  42. (expires . #<date nanosecond: 0 second: 31 minute: 30 hour: 13 day: 2 month: 6 year: 2019 zone-offset: 0>)
  43. (cache-control no-cache)
  44. (accept-ranges bytes))
  45. |#
  46. #|
  47. If you already have a port open, pass it as port. Otherwise, a
  48. connection will be opened to the server corresponding to uri. Any
  49. extra headers in the alist headers will be added to the request.
  50. If body is not #f, a message body will also be sent with the HTTP
  51. request. If body is a string, it is encoded according to the
  52. content-type in headers, defaulting to UTF-8. Otherwise body should be
  53. a bytevector, or #f for no body. Although a message body may be sent
  54. with any request, usually only POST and PUT requests have bodies.
  55. If decode-body? is true, as is the default, the body of the response
  56. will be decoded to string, if it is a textual content-type. Otherwise
  57. it will be returned as a bytevector.
  58. However, if streaming? is true, instead of eagerly reading the
  59. response body from the server, this function only reads off the
  60. headers. The response body will be returned as a port on which the
  61. data may be read.
  62. Unless keep-alive? is true, the port will be closed after the full
  63. response body has been read.
  64. Returns two values: the response read from the server, and the
  65. response body as a string, bytevector, #f value, or as a port (if
  66. streaming? is true).
  67. |#