Changeset 38139 in project


Ignore:
Timestamp:
01/20/20 08:54:05 (8 months ago)
Author:
sjamaan
Message:

Add example to download files to http-client docs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/5/http-client

    r37991 r38139  
    169169===== Examples
    170170
     171====== Simple GET request
     172
     173This is a simple GET request which retrieves the main index page of this wiki into a string:
     174
    171175<enscript highlight="scheme">
    172176(import http-client (chicken io))
    173177
    174 ;; Start with a simple GET request:
    175178(with-input-from-request "http://wiki.call-cc.org/" #f read-string)
    176179 => ;; [the chicken wiki page HTML contents]
     180</enscript>
     181
     182====== Downloading a file onto disk
     183
     184This is no different from a regular GET request, but in this example we'll use [[/man/5/Module (chicken port)#copy-port|copy-port]] to copy the data from our HTTP response port into a file port.
     185
     186<enscript highlight="scheme">
     187(import http-client (chicken port))
     188
     189(call-with-output-file
     190  "/tmp/target.png"
     191  (lambda (out)
     192    (call-with-input-request
     193      "http://example.com/file.png"
     194      #f                                                                                                                                 
     195      (lambda (in) (copy-port in out)))))
     196</enscript>
     197
     198====== Simple POST and PUT requests
     199
     200A POST request requires nothing special, we can use an alist as the second argument and http-client will understand you want to perform a POST request with the alist serialized as form data into the POST body.
     201
     202A PUT request is less common so it requires us to craft a request object.
     203
     204<enscript highlight="scheme">
     205(import http-client (chicken io))
    177206
    178207;; Perform a POST of the key "test" with value "value" to an echo service:
     
    184213;; constructing your request object manually:
    185214
    186 (import intarweb uri-common (chicken io))  ; uri-common gives us "make-request" and "uri-reference"
     215(import intarweb uri-common)  ; uri-common gives us "uri-reference", "make-request" is from intarweb
    187216
    188217(with-input-from-request
     
    191220  (lambda () (print "Page contents"))
    192221  read-string)
     222</enscript>
     223
     224======= Performing a PUT request with JSON contents
     225
     226<enscript highlight="scheme">
     227(import http-client (chicken io))
    193228
    194229;; Performing a JSON PUT request furthermore requires you to
     
    198233                          uri: uri
    199234                          headers: (headers '((content-type application/json))))))
    200   (with-input-from-request req "Contents of the document" read-string))
     235  (with-input-from-request req "[\"Contents of the document\"]" read-string))
     236</enscript>
     237
     238====== Sending multi-part (file upload) requests
     239
     240<enscript highlight="scheme">
     241(import http-client (chicken io))
    201242
    202243;; Finally, an example where we need to send an "attachment" (file)
     
    595636=== License
    596637
    597   Copyright (c) 2008-2019, Peter Bex
     638  Copyright (c) 2008-2020, Peter Bex
    598639  Parts copyright (c) 2000-2004, Felix L. Winkelmann
    599640  All rights reserved.
Note: See TracChangeset for help on using the changeset viewer.