···6060 (* Consume remaining body if any *)
6161 try
6262 (* Read and discard remaining data by copying to a buffer *)
6363+ (* TODO make this a more efficient null sink *)
6364 let buf = Buffer.create 4096 in
6465 Eio.Flow.copy t.body (Eio.Flow.buffer_sink buf)
6566 with _ -> ();
···8990 elapsed: %.3fs@,\
9091 @[%a@]@]"
9192 t.status status_desc t.url t.elapsed
9292- Headers.pp t.headers9393+ Headers.pp t.headers
+3-40
stack/requests/lib/session.ml
···250250 in
251251 execute_request t ?headers ?auth ?timeout ~method_:`GET url
252252253253-let post t ?headers ?body ?auth ?timeout ?json ?form url =
254254- let body, headers = match json, form, body with
255255- | Some json, _, _ ->
256256- let json_str = Yojson.Safe.to_string json in
257257- let body = Body.json json_str in
258258- let headers = Option.value headers ~default:Headers.empty
259259- |> Headers.content_type Mime.json in
260260- Some body, Some headers
261261- | _, Some form, _ ->
262262- let form_str =
263263- form
264264- |> List.map (fun (k, v) -> Uri.pct_encode k ^ "=" ^ Uri.pct_encode v)
265265- |> String.concat "&"
266266- in
267267- let body = Body.text form_str in
268268- let headers = Option.value headers ~default:Headers.empty
269269- |> Headers.content_type Mime.form in
270270- Some body, Some headers
271271- | _, _, body -> body, headers
272272- in
253253+let post t ?headers ?body ?auth ?timeout url =
273254 execute_request t ?headers ?body ?auth ?timeout ~method_:`POST url
274255275275-let put t ?headers ?body ?auth ?timeout ?json url =
276276- let body, headers = match json with
277277- | Some json ->
278278- let json_str = Yojson.Safe.to_string json in
279279- let body = Body.json json_str in
280280- let headers = Option.value headers ~default:Headers.empty
281281- |> Headers.content_type Mime.json in
282282- Some body, Some headers
283283- | None -> body, headers
284284- in
256256+let put t ?headers ?body ?auth ?timeout url =
285257 execute_request t ?headers ?body ?auth ?timeout ~method_:`PUT url
286258287287-let patch t ?headers ?body ?auth ?timeout ?json url =
288288- let body, headers = match json with
289289- | Some json ->
290290- let json_str = Yojson.Safe.to_string json in
291291- let body = Body.json json_str in
292292- let headers = Option.value headers ~default:Headers.empty
293293- |> Headers.content_type Mime.json in
294294- Some body, Some headers
295295- | None -> body, headers
296296- in
259259+let patch t ?headers ?body ?auth ?timeout url =
297260 execute_request t ?headers ?body ?auth ?timeout ~method_:`PATCH url
298261299262let delete t ?headers ?auth ?timeout url =
+15-8
stack/requests/lib/session.mli
···159159 ?body:Body.t ->
160160 ?auth:Auth.t ->
161161 ?timeout:Timeout.t ->
162162- ?json:Yojson.Safe.t ->
163163- ?form:(string * string) list ->
164162 string ->
165163 Response.t
166166-(** POST request with optional JSON or form data *)
164164+(** POST request with optional body *)
167165168166val put :
169167 (_ Eio.Time.clock, _ Eio.Net.t) t ->
···171169 ?body:Body.t ->
172170 ?auth:Auth.t ->
173171 ?timeout:Timeout.t ->
174174- ?json:Yojson.Safe.t ->
175172 string ->
176173 Response.t
177177-(** PUT request with optional JSON data *)
174174+(** PUT request with optional body *)
178175179176val patch :
180177 (_ Eio.Time.clock, _ Eio.Net.t) t ->
···182179 ?body:Body.t ->
183180 ?auth:Auth.t ->
184181 ?timeout:Timeout.t ->
185185- ?json:Yojson.Safe.t ->
186182 string ->
187183 Response.t
188188-(** PATCH request with optional JSON data *)
184184+(** PATCH request with optional body *)
189185190186val delete :
191187 (_ Eio.Time.clock, _ Eio.Net.t) t ->
···289285 ]}
290286*)
291287288288+(** {2 Posting JSON Data}
289289+ {[
290290+ let session = Session.create ~sw env in
291291+ let json_str = {|{"name": "John", "age": 30}|} in
292292+ let response = Session.post session
293293+ ~body:(Body.json json_str)
294294+ "https://api.example.com/users" in
295295+ (* Response handling *)
296296+ ]}
297297+*)
298298+292299(** {2 With Authentication}
293300 {[
294301 let session = Session.create ~sw env in
···307314308315 (* Login - cookies will be saved *)
309316 let login = Session.post session
310310- ~form:["username", "user"; "password", "pass"]
317317+ ~body:(Body.form ["username", "user"; "password", "pass"])
311318 "https://example.com/login" in
312319313320 (* Access protected resource - cookies are automatically included *)