@@ -88,13 +88,15 @@ Once you have a Mojo project set up locally,
8888 @value
8989 struct Printer(HTTPService):
9090 fn func(self, req: HTTPRequest) raises -> HTTPResponse:
91- var uri = req.uri()
92- print("Request URI: ", to_string(uri.request_uri()))
93-
94- var header = req.header
95- print("Request protocol: ", header.protocol_str())
96- print("Request method: ", to_string(header.method()))
97- print("Request Content-Type: ", to_string(header.content_type()))
91+ var uri = req.uri
92+ print("Request URI: ", to_string(uri.request_uri))
93+
94+ var header = req.headers
95+ print("Request protocol: ", req.protocol)
96+ print("Request method: ", req.method)
97+ print(
98+ "Request Content-Type: ", to_string(header[HeaderKey.CONTENT_TYPE])
99+ )
98100
99101 var body = req.body_raw
100102 print("Request Body: ", to_string(body))
@@ -103,9 +105,11 @@ Once you have a Mojo project set up locally,
103105 ```
1041066 . Start a server listening on a port with your service like so.
105107 ``` mojo
108+ from lightbug_http import Welcome, SysServer
109+
106110 fn main() raises:
107111 var server = SysServer()
108- var handler = Printer ()
112+ var handler = Welcome ()
109113 server.listen_and_serve("0.0.0.0:8080", handler)
110114 ```
111115Feel free to change the settings in `listen_and_serve()` to serve on a particular host and port.
@@ -123,15 +127,15 @@ from lightbug_http import *
123127struct ExampleRouter(HTTPService):
124128 fn func(self, req: HTTPRequest) raises -> HTTPResponse:
125129 var body = req.body_raw
126- var uri = req.uri()
130+ var uri = req.uri
127131
128- if uri.path() == "/":
132+ if uri.path == "/":
129133 print("I'm on the index path!")
130- if uri.path() == "/first":
134+ if uri.path == "/first":
131135 print("I'm on /first!")
132- elif uri.path() == "/second":
136+ elif uri.path == "/second":
133137 print("I'm on /second!")
134- elif uri.path() == "/echo":
138+ elif uri.path == "/echo":
135139 print(to_string(body))
136140
137141 return OK(body)
@@ -152,21 +156,21 @@ from lightbug_http import *
152156@value
153157struct Welcome(HTTPService):
154158 fn func(self, req: HTTPRequest) raises -> HTTPResponse:
155- var uri = req.uri()
159+ var uri = req.uri
156160
157- if uri.path() == "/":
161+ if uri.path == "/":
158162 var html: Bytes
159163 with open("static/lightbug_welcome.html", "r") as f:
160164 html = f.read_bytes()
161165 return OK(html, "text/html; charset=utf-8")
162-
163- if uri.path() == "/logo.png":
166+
167+ if uri.path == "/logo.png":
164168 var image: Bytes
165169 with open("static/logo.png", "r") as f:
166170 image = f.read_bytes()
167171 return OK(image, "image/png")
168-
169- return NotFound(uri.path() )
172+
173+ return NotFound(uri.path)
170174```
171175
172176### Using the client
@@ -178,33 +182,34 @@ from lightbug_http import *
178182from lightbug_http.sys.client import MojoClient
179183
180184fn test_request(inout client: MojoClient) raises -> None:
181- var uri = URI("http://httpbin.org/status/404")
182- try:
183- uri.parse()
184- except e:
185- print("error parsing uri: " + e.__str__())
186-
185+ var uri = URI.parse_raises("http://httpbin.org/status/404")
186+ var headers = Header("Host", "httpbin.org")
187187
188- var request = HTTPRequest(uri)
189- var response = client.do(request)
188+ var request = HTTPRequest(uri, headers )
189+ var response = client.do(request^ )
190190
191191 # print status code
192- print("Response:", response.header. status_code() )
192+ print("Response:", response.status_code)
193193
194194 # print parsed headers (only some are parsed for now)
195- print("Content-Type:", to_string( response.header.content_type()) )
196- print("Content-Length", response.header.content_length() )
197- print("Server:", to_string(response.header.server() ))
195+ print("Content-Type:", response.headers["Content-Type"] )
196+ print("Content-Length", response.headers["Content-Length"] )
197+ print("Server:", to_string(response.headers["Server"] ))
198198
199- print("Is connection set to connection-close? ", response.header.connection_close())
199+ print(
200+ "Is connection set to connection-close? ", response.connection_close()
201+ )
200202
201203 # print body
202- print(to_string(response.get_body_bytes() ))
204+ print(to_string(response.body_raw ))
203205
204206
205- fn main() raises -> None:
206- var client = MojoClient()
207- test_request(client)
207+ fn main() -> None:
208+ try:
209+ var client = MojoClient()
210+ test_request(client)
211+ except e:
212+ print(e)
208213```
209214
210215Pure Mojo-based client is available by default. This client is also used internally for testing the server.
0 commit comments