2021-01-06 15:44

Extension point: transformRequest

A nice mechanism to transform a request before it is sent is the dual to the existing transformResponse. (suggestion: transformRequest) This would allow extensions that primarily implement these two extension points to create drivers that would allow non-htmx friendly backends to be used transparently.

Real-world example: I wanted to add a "Range" header to requests, but ended up using a global listener on "htmx:configRequest" and some conditionals on the element in question. This paired up with a specific raw-to-json -style extension particular to the backend I was using. Being able to bundle the two into a single concept would simplify implementations.

Example: I can imagine writing a driver for various HTTP backends (think postgrest) that need transformation of both the request and response in order to function with HTMX.


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_39867066 weixin_39867066 4月前

    I have come across another use case where this would be very helpful. I'm currently using cookies to sign users in to my web application. But, I'd really like to use localStorage instead. But, this would require some extension that could find the users session token (JWT in this case) and insert it into each request before it is sent to the server. This is not currently possible with htmx, but it would be very easy to accomplish once we have this extension point.

    点赞 评论 复制链接分享
  • weixin_39867066 weixin_39867066 4月前

    Hey -- after I wrote this PR and dug in to the code a little more, I discovered there's already a way to do this using events. The htmx:beforeRequest event allows you to manipulate the request configuration before it is sent to the server.

    Does this work for you? Are you comfortable closing this issue?

    点赞 评论 复制链接分享
  • weixin_39970166 weixin_39970166 4月前

    I am going to close it, the htmx:beforeRequest event now handles this pretty completely and gracefully

    点赞 评论 复制链接分享
  • weixin_39746282 weixin_39746282 4月前

    The global events can handle the need(I used configRequest in my use case), but I don’t believe it scales. The goal would be to be able to write an extension that can do this. This would also round out the API for extensions.

    Or did something change or get added that I’m not aware of?

    点赞 评论 复制链接分享