0

I have a F# console app (.Net 5) sending http requests via FSharp.Data and I haven't found a way to log the raw http requests. I tried using the F# .Net 6 web app template and enabling HttpLogging, but this won't log my http requests executed in the code. Other tools like fiddler or wireshark are not an option because of work policies.

Is there a way to enable such logging, for example in this minimal example console app?

open FSharp.Data

[<EntryPoint>]
let main argv =
    let html = Http.Request("http://tomasp.net")
    printfn "%s" (html.ToString())
    0

1 Answer 1

2

You can pass a customization function to Http.Request, I think it would be OK to pass one that has a side effect of logging something about the request. For example:

open System.Net
open FSharp.Data

let requestHeaders =
    [
        "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
        "Accept", "*/*"
    ]

let requestCustomizeFunc =
    fun (request: HttpWebRequest) -> 
        printfn "Address: %O" request.Address
        printfn "Method: %O" request.Method
        printfn "Headers: %O" request.Headers
        request

[<EntryPoint>]
let main argv =
    let response = Http.Request ("http://tomasp.net", headers = requestHeaders, customizeHttpRequest = requestCustomizeFunc)

    response.StatusCode
    |> printfn "%O"

    0

This is quick and dirty; if you're really serious about logging what's going on at the network level in your .NET app, you probably want to consider taking advantage of the built-in tracing capability.

https://learn.microsoft.com/en-us/dotnet/framework/network-programming/network-tracing

Sign up to request clarification or add additional context in comments.

1 Comment

I think that should be enough for my case. I even used the customizeHttpRequest already in my code but did not think of using it this way. Thanks for pointing this out and giving some source in case I need to dig deeper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.