简体   繁体   中英

Setting a blank custom header in ruby on rails

I'm writing a REST server using Ruby on rails.

Some of the communication with the client is by setting certain custom headers to predefined values. In certain situations the server is expected to respond with a custom header that is an empty string. My problem is that rails seems to omit any blank headers from the response (it will even omit a whitespace header).

This is my current code:

config.action_dispatch.default_headers = {
  'X-WOPI-EmptyLock' => "",
  'X-WOPI-FullLock' => "astring",
  'Random-header' => "with value",
  'Random-empty-header' => ""
}

But the response will only contain headers which are not empty strings:

Resulting headers in response Curl response headers

Is there some way to configure rails to still send headers even if they are empty?

You can set your custom headers in config/application.rb file

Ex:

config.action_dispatch.default_headers = {
  'Custom-Header1' => "", # without value
  'Custom-Header2' => "with value"
}

Note: restart rails server after made changes

In response

Custome-Header1 →
Custome-Header2 →with value

Updated answer

Here is my controller code as below

 def home

    response.headers["My-Custome-Header"] = "With value"
    response.headers["My-Custome-Header-2"] = "" # Without value

    render json: {data: "It's working my friend"}
  end

And required output as shown in screenshot

在此输入图像描述

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM