最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent url encode in response set cookie - nodejs - Stack Overflow

programmeradmin0浏览0评论

In my node application, I am trying to change my cookie value with the below code. But when I set some cookie values I see it modified in my response header of browser. Node code :

let nonEncodedString ='s%3A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%2FFT3TM64riv3QAs'
res.cookie('connect.sid', nonEncodedString , { maxAge, httpOnly: true, overwrite: true });

But the header I get is

set-cookie: connect.sid=s%253A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%252FFT3TM64riv3QAs; Max-Age=157680000; Path=/; Expires=Thu, 31 Jul 2025 11:28:35 GMT; HttpOnly

essentially s%3A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%2FFT3TM64riv3QAs is changed to s%253A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%252FFT3TM64riv3QAs. ie. '25' is being added.

I think it's happening because it is getting URL encoded. I don't want that to happen since its changing the value I am sending and I don't have control to parse it before the browser sets it in the cookie.

In my node application, I am trying to change my cookie value with the below code. But when I set some cookie values I see it modified in my response header of browser. Node code :

let nonEncodedString ='s%3A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%2FFT3TM64riv3QAs'
res.cookie('connect.sid', nonEncodedString , { maxAge, httpOnly: true, overwrite: true });

But the header I get is

set-cookie: connect.sid=s%253A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%252FFT3TM64riv3QAs; Max-Age=157680000; Path=/; Expires=Thu, 31 Jul 2025 11:28:35 GMT; HttpOnly

essentially s%3A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%2FFT3TM64riv3QAs is changed to s%253A9Q8kumq4BgrHtJPM90ebhhl6OqChsxdp.x0uf93Hk5I03KWeF%252FFT3TM64riv3QAs. ie. '25' is being added.

I think it's happening because it is getting URL encoded. I don't want that to happen since its changing the value I am sending and I don't have control to parse it before the browser sets it in the cookie.

Share Improve this question asked Aug 1, 2020 at 13:14 AnandShivaAnandShiva 1,36919 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

you should set an encode function:

  res.cookie('connect.sid', nonEncodedString,
    { maxAge,
      httpOnly: true,
      overwrite: true,
      encode: v => v
    })

the default encode function is encodeURLComponent.

发布评论

评论列表(0)

  1. 暂无评论