計算機科学のブログ

Haskell - 実践Haskell - HaskellでのHTTPリクエストの作成 - エラーの出力

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT 7(実践Haskell)、LESSON 39(HaskellでのHTTPリクエストの作成)、39.6(練習問題)、Q39-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
- http-types

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
import Network.HTTP.Types.Status

main :: IO ()
main = do
    response <- httpLBS "http://www.example.com"
    let status = getResponseStatus response
    let code = statusCode status
    print code
    print $ statusMessage status
    if code == 200
        then
            putStrLn "saving request to file"
        else
            print $ statusMessage status

入出力結果(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-DVrs0PX0l2kFHXG9t5CFgH
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..
200
"OK"
saving request to file
%