Various ways to use response.setHeader

The response.setHeader() method is used to set the value of HTTP response headers. It can achieve different functionalities by setting different parameters. Here are some common uses of response.setHeader().

  1. Set the value of a single HTTP response header:
    response.setHeader(“HeaderName”, “HeaderValue”);
    For example: response.setHeader(“Content-Type”, “application/json”);
  2. Setting multiple values for HTTP response headers:
    response.setHeader(“HeaderName1”, “HeaderValue1”);
    response.setHeader(“HeaderName2”, “HeaderValue2”);

    For example: response.setHeader(“Content-Type”, “application/json”);
    response.setHeader(“Cache-Control”, “no-cache”);
  3. Specify the URL for redirection:
    response.setHeader(“Location”, “redirectURL”);
    For example: response.setHeader(“Location”, “https://example.com”);
  4. Set file download by adding the line below in the code:
    response.setHeader(“Content-Disposition”, “attachment; filename=filename.ext”);
    For example: response.setHeader(“Content-Disposition”, “attachment; filename=test.pdf”);

Here are several common ways to use response.setHeader(), you can choose the appropriate method based on your specific needs to set the value of the HTTP response header.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds