Unable to bind to request

Hi @mythz,

Hope you are doing great. Here is the GET request

http://localhost:5283/api/MainCategoryByCodeRequest?categoryName=men&RequestTimestamp=1700064350&channelId=1

If you examine the RequestTimestamp value it is appearing as numeric which is wrong and in my RequestDTO it is defined as DateTime.

When I execute this request it is throwing Unable to bind to request which is understood and I am landing into this method UncaughtExceptionHandlers

I want to log this request/response using the RequestLogsFeature.

Here is the piece of code from my AppHost file.

Plugins.Add(new RequestLogsFeature
 {
     EnableRequestBodyTracking = true,
     EnableResponseTracking = true,
     EnableErrorTracking = true,
     AccessRole = "Admin",
     RequestLogger = new CsvRequestLogger(
     files: new FileSystemVirtualFiles(HostContext.Config.WebHostPhysicalPath),
     requestLogsPattern: "requestLogs/{year}-{month}-{day}.csv",
     appendEvery: TimeSpan.FromSeconds(1)),
 });

This piece of code is working when the request successfully reaches to service, because request is not able to bind properly I guess due to which it is not even entering into the request pipeline/lifecycle.

May I kindly request you to please guide me that using RequestLogger in this situation how can I log this request and the response (I am returning custom response) returned to the consumer in the same log file.

Thanks

Hi @kshahzad ,

I’m not sure if you provided other related code like your Request/Response DTOs via a different channel but for completeness it would be good to have here as well.

When value types like DateTime travel over the wire or are represented in something like a Query String value, it needs to be serialized. In this case, the value 1700064350 looks like Unix timestamp which is the number of seconds since 1970.

You can configure the DateHandler (which my default is TimestampOffset) to use UnixTime application wide by using the config in your AppHost:

JsConfig.Init(new Config {
    DateHandler = DateHandler.UnixTime
});

That is likely, depending on your apps configuration, the binding of the property could be failing in a way that is causing the issue. If you create a minimal reproduction, I can take a look.

Hope that helps.

Hi @layoric,

From my side I thought I have described in great detail but seems I didn’t. Apologize if I confused you guys. My issues is resolved and the date is working fine.

But as I mentioned earlier, can you guide me if there is any way that when due to some exception I am landing into UncaughtExceptionHandlers method how can I log this request/response using RequestLogsFeature into the same csv file. I need to log the request/response

Thanks

If the exception is fired from UncaughtExceptionHandlers then the request is unknown since it’s unable to create a Request DTO.

You can try manually logging the entry by attempting to recreate the Request DTO from the URL:

UncaughtExceptionHandlers.Add((req, res, operationName, ex) => {
    object? requestDto = null;
    try
    {
        // Try create Request DTO from URL
        requestDto = HostContext.Metadata.CreateRequestFromUrl(req.AbsoluteUri);
    }
    catch (Exception)
    {
        try
        {
            // Try create Request DTO from URL without QueryString
            requestDto = HostContext.Metadata.CreateRequestFromUrl(req.AbsoluteUri.LeftPart('?'));
        }
        catch (Exception)
        {
            // Cannot create Request DTO, wont be able to log Request
            return;
        }
    }
    HostContext.GetPlugin<RequestLogsFeature>().RequestLogger
        .Log(req, requestDto, ex, req.GetElapsed());
});

But this wont be an accurate recreation of the Request DTO as it’ll only be able to create the Request DTO from the /path/info and QueryString and wont be able to re-parse the HTTP Request body, if that fails (e.g. from an invalid QueryString) it’ll try recreating an empty Request DTO without parsing the QueryString, otherwise, if that fails it wont be able to recreate the Request DTO to be able to log it.