This expression was expected to have type MemoryStream but here has type StreamWriter?

StackOverflow https://stackoverflow.com/questions/19097237

  •  29-06-2022
  •  | 
  •  

Question

I'm trying to add a feature to the Http module (https://github.com/fsharp/FSharp.Data/blob/master/src/Library/Http.fs) of FSharp.Data to download and save a file (from a web response stream). I modified the function asyncReadToEnd but got the following error. Whats the best way to define the function asyncReadToEnd so it will write to MemoryStream if streamWriter is not provided.

Error   1   This expression was expected to have type
    MemoryStream    
but here has type
    StreamWriter    

Error line. (at Some sw)

use sw = new StreamWriter(fileName)
let! response = Http.InnerRequest(url, true, ?headers=headers, ?query=query, ?meth=meth, ?body=body, ?cookies=cookies, ?cookieContainer=cookieContainer, ?certificate=certificate, ?streamWriter=Some sw)

Code snippet:

  /// Read the contents of a stream asynchronously and return it as a string
  static let asyncReadToEnd (stream:Stream) isText streamWriter = async {
    // Allocate 4kb buffer for downloading dat
    let buffer = Array.zeroCreate (4 * 1024)
    use output = 
        match streamWriter with
        | Some (sw) -> sw
        | None -> new MemoryStream()
    let reading = ref true
    ....

  static member private InnerRequest(url:string, forceText, ?query, ?headers, ?meth, ?body, ?cookies, ?cookieContainer, ?certificate, ?streamWriter) = async {
    ....
    return! Http.augmentWebExceptionsWithDetails <| fun () -> async {
      use! resp = Async.FromBeginEnd(req.BeginGetResponse, req.EndGetResponse)
      use stream = resp.GetResponseStream()
      let! respBody = asyncReadToEnd stream (forceText || (isText resp.ContentType)) streamWriter
      let cookies = Map.ofList [ for cookie in cookieContainer.GetCookies uri |> Seq.cast<Cookie> -> cookie.Name, cookie.Value ]  
      let headers = Map.ofList [ for header in resp.Headers.AllKeys -> header, resp.Headers.[header] ]
      let statusCode = 
        match resp with
        | :? HttpWebResponse as resp -> int resp.StatusCode
        | _ -> 0
      return { Body = respBody
               Headers = headers
               ResponseUrl = resp.ResponseUri.OriginalString
               Cookies = cookies
               StatusCode = statusCode } }
  }

  static member AsyncRequestFile(url, (fileName:string), ?query, ?headers, ?meth, ?body, ?cookies, ?cookieContainer, ?certificate) = async {
    use sw = new StreamWriter(fileName)
    let! response = Http.InnerRequest(url, true, ?headers=headers, ?query=query, ?meth=meth, ?body=body, ?cookies=cookies, ?cookieContainer=cookieContainer, ?certificate=certificate, ?streamWriter=Some sw)
    return
        match response.Body with
        | ResponseBody.Text text -> text
        | ResponseBody.Binary binary -> failwithf "Expecting text, but got a binary response (%d bytes)" binary.Length
  }
Was it helpful?

Solution

The issue appears to be that in:

use output = 
        match streamWriter with
        | Some (sw) -> sw
        | None -> new MemoryStream()

streamWriter is being inferred to be a MemoryStream option and therefore so is the streamWriter parameter of InnerRequest.

When you call it in AsyncRequestFile you are passing a StreamReader option instead, hence the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top