Indeed the issue is with your call to pipe-content-length which is to be
used with pipes and is not the Content-Length header returned when doing
HTTP requests.
The Content-Length can be found in the list of headers returned by the call
to http-sendrecv (second value or b in your let-values call). You can parse
these using the net/head module.

HTH
Philippe



On Tue, Oct 16, 2018 at 9:42 PM Chansey <[email protected]> wrote:

> Hi Philippe
>
> I now use (copy-port ip op), it works.
>
> Thanks.
>
> But why my code above can not work? ( content-length wrong?)
>
>
>
> On Wednesday, October 17, 2018 at 3:30:05 AM UTC+8, Philippe M wrote:
>>
>> Hi
>>
>> You could simply do a (copy-port ip op) and remove the  content-length
>> and buffer parts i think.
>>
>>
>> Le mar. 16 oct. 2018 à 21:17, Chansey <[email protected]> a écrit :
>>
>>>
>>> Dear all,
>>>
>>> I wrote some experiment code for net/http-client.
>>>
>>> The purpose is downloading some images from web.
>>>
>>> This is my code (run on windows7 using DrRacket, version 6.12):
>>>
>>> #lang racket
>>>
>>> (require net/http-client)
>>> (require "./racket-pretty-bytes/main.rkt")
>>>
>>> (define ip (let-values (((a b c) (http-sendrecv "www.google.com"
>>>
>>> "/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png")))
>>>
>>>              c))
>>>
>>> ;; different/wrong size per call???
>>> (define content-length (pipe-content-length ip))
>>> (println content-length)
>>>
>>> (let ([buffer (make-bytes content-length)])
>>>   (read-bytes! buffer ip 0 content-length)
>>>   (pretty-print-bytes buffer)
>>>
>>>   (let ((op (open-output-file "google.png")))
>>>     (write buffer op)
>>>     (close-output-port op))
>>>   )
>>>
>>>
>>> The problem is the content-length wrong, so the download file
>>> "google.png" can not be open by image viewer.
>>>
>>> Is there any reference code or tutorial for downloading image and save
>>> files using http-client ?
>>>
>>> Thanks.
>>>
>>> Br,
>>> Chansey
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to