Haskell - 実践Haskell - HaskellでのHTTPリクエストの作成 - token, host, method, path, SSL
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT 7(実践Haskell)、LESSON 39(HaskellでのHTTPリクエストの作成)、39.6(練習問題)、Q39-1の解答を求めてみる。
軽量マークアップ言語
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
myToken :: BC.ByteString
myToken = "<API トークン>"
noaaHost :: BC.ByteString
noaaHost = "www.ncdc.noaa.gov"
apiPath :: BC.ByteString
apiPath = "/cdo-Web/api/v2/datasets"
buildRequestsNOSSL :: BC.ByteString -> BC.ByteString -> BC.ByteString
-> BC.ByteString -> Request
buildRequestsNOSSL token host method path =
setRequestHeader "token" [token] $
setRequestHost host $
setRequestMethod method $
setRequestPath path $
setRequestSecure False $
setRequestPort 443 $
defaultRequest
request :: Request
request = buildRequestsNOSSL myToken noaaHost "GET" apiPath
main :: IO ()
main = do
print request
入出力結果(Terminal, Zsh)
% stack run
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..
Request {
host = "www.ncdc.noaa.gov"
port = 443
secure = False
requestHeaders = [("token","<API \200\252\175\243>")]
path = "/cdo-Web/api/v2/datasets"
queryString = ""
method = "GET"
proxy = Nothing
rawBody = False
redirectCount = 10
responseTimeout = ResponseTimeoutDefault
requestVersion = HTTP/1.1
proxySecureMode = ProxySecureWithConnect
}
%