Haskell - 実践Haskell - HaskellでのHTTPリクエストの作成 - HTTP.Simpleモジュールを使用する
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT 7(実践Haskell)、LESSON 39(HaskellでのHTTPリクエストの作成)、39.2(HTTP.Simpleモジュールを使用する)、クイックチェック 39-2の解答を求めてみる。
軽量マークアップ言語
http-lesson/package.yaml
name: http-lesson
version: 0.1.0.0
github: "githubuser/http-lesson"
license: BSD-3-Clause
author: "Author name here"
maintainer: "example@example.com"
copyright: "2025 Author name here"
extra-source-files:
- README.md
- CHANGELOG.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/http-lesson#readme>
dependencies:
- base >= 4.7 && < 5
- bytestring
- http-conduit
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
default-extensions:
- OverloadedStrings
library:
source-dirs: src
executables:
http-lesson-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- http-lesson
tests:
http-lesson-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- http-lesson
コード
http-lesson/app/Main.hs
module Main (main) where
-- import qualified Data.ByteString as B
-- import qualified Data.ByteString.Char8 as BC
-- import qualified Data.ByteString.Lazy as L
-- import qualified Data.ByteString.Lazy.Char8 as LC
import Network.HTTP.Simple
main :: IO ()
main = do
let response = httpLBS "http://news.ycombinator.com"
let headersIO1 = getResponseHeaders <$> response
headers1 <- headersIO1
print headers1
res <- response
let headers2 = getResponseHeaders res
print headers2
入出力結果(Terminal, Zsh)
% stack run
http-lesson-0.1.0.0: unregistering (local file changes: app/Main.hs)
http-lesson> build (lib + exe) with ghc-9.8.4
Preprocessing library for http-lesson-0.1.0.0..
Building library for http-lesson-0.1.0.0..
Preprocessing executable 'http-lesson-exe' for http-lesson-0.1.0.0..
Building executable 'http-lesson-exe' for http-lesson-0.1.0.0..
[1 of 2] Compiling Main [Source file changed]
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.8.4/build/http-lesson-exe/http-lesson-exe [Objects changed]
ld: warning: -U option is redundant when using -undefined dynamic_lookup
http-lesson> copy/register
Installing library in /Users/.../http-lesson/.stack-work/install/aarch64-osx/e2dcdb9c07548bf6dae5f9b6f2750a6303aa3357cf62332e90692b200e5825d0/9.8.4/lib/aarch64-osx-ghc-9.8.4/http-lesson-0.1.0.0-GvOmP0p2gw480pL83GZ8q7
Installing executable http-lesson-exe in /Users/.../http-lesson/.stack-work/install/aarch64-osx/e2dcdb9c07548bf6dae5f9b6f2750a6303aa3357cf62332e90692b200e5825d0/9.8.4/bin
Registering library for http-lesson-0.1.0.0..
[("Server","nginx"),("Date","Sat, 05 Apr 2025 02:30:07 GMT"),("Content-Type","text/html; charset=utf-8"),("Transfer-Encoding","chunked"),("Connection","keep-alive"),("Vary","Accept-Encoding"),("Cache-Control","private; max-age=0"),("X-Frame-Options","DENY"),("X-Content-Type-Options","nosniff"),("X-XSS-Protection","1; mode=block"),("Referrer-Policy","origin"),("Strict-Transport-Security","max-age=31556900"),("Content-Security-Policy","default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/ https://cdnjs.cloudflare.com/; frame-src 'self' https://www.google.com/recaptcha/; style-src 'self' 'unsafe-inline'; img-src 'self' https://account.ycombinator.com; frame-ancestors 'self'"),("Content-Encoding","gzip")]
[("Server","nginx"),("Date","Sat, 05 Apr 2025 02:30:07 GMT"),("Content-Type","text/html; charset=utf-8"),("Transfer-Encoding","chunked"),("Connection","keep-alive"),("Vary","Accept-Encoding"),("Cache-Control","private; max-age=0"),("X-Frame-Options","DENY"),("X-Content-Type-Options","nosniff"),("X-XSS-Protection","1; mode=block"),("Referrer-Policy","origin"),("Strict-Transport-Security","max-age=31556900"),("Content-Security-Policy","default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/ https://cdnjs.cloudflare.com/; frame-src 'self' https://www.google.com/recaptcha/; style-src 'self' 'unsafe-inline'; img-src 'self' https://account.ycombinator.com; frame-ancestors 'self'"),("Content-Encoding","gzip")]
%