How to use HTTP Request Header
An HTTP request header is a piece of metadata sent by a client (such as a web browser or API client) to a server when making an HTTP request. Headers provide additional information about the request, such as authentication details, content type, user agent, and more.
This feature is used when rendering a URL to PDF, allowing you to provide HTTP header information when making the request.
Get started with IronPDF
Start using IronPDF in your project today with a free trial.
How to use HTTP Request Header
- Download IronPDF from NuGet
- Prepare the HTTP request headers as a C# dictionary
- Assign the dictionary to the HttpRequestHeaders property
- Render the URL to PDF using the
RenderUrlAsPdf
method - Save the PDF as a file or export it as bytes
Use HTTP Request Header Example
Before using the HttpRequestHeaders property to set an HTTP request header, first design a proper HTTP request header object. During the rendering process, this header will be included in the URL request sent to the server. As an example, we will use httpbin.org, a website that helps show the headers request.
:path=/static-assets/pdf/content-code-examples/how-to/http-request-header.cs
// Import the necessary namespaces for IronPDF and collections using IronPdf; using System.Collections.Generic; // Create an instance of the ChromePdfRenderer, which will be used to render HTML pages or URLs to PDF. var renderer = new ChromePdfRenderer(); // Set HttpRequestHeaders to include an authorization header. This is useful when accessing pages that require authentication. renderer.RenderingOptions.HttpRequestHeaders = new Dictionary<string, string> { { "Authorization", "Bearer test-token-123" } }; // Render a PDF from a URL that requires an authentication token. // In this example, https://httpbin.org/bearer is a site that can test if the bearer token header is being passed correctly. var pdf = renderer.RenderUrlAsPdf("https://httpbin.org/bearer"); // Save the generated PDF to a file named "output.pdf". pdf.SaveAs("output.pdf");
' Import the necessary namespaces for IronPDF and collections Imports IronPdf Imports System.Collections.Generic ' Create an instance of the ChromePdfRenderer, which will be used to render HTML pages or URLs to PDF. Private renderer = New ChromePdfRenderer() ' Set HttpRequestHeaders to include an authorization header. This is useful when accessing pages that require authentication. renderer.RenderingOptions.HttpRequestHeaders = New Dictionary(Of String, String) From { {"Authorization", "Bearer test-token-123"} } ' Render a PDF from a URL that requires an authentication token. ' In this example, https://httpbin.org/bearer is a site that can test if the bearer token header is being passed correctly. Dim pdf = renderer.RenderUrlAsPdf("https://httpbin.org/bearer") ' Save the generated PDF to a file named "output.pdf". pdf.SaveAs("output.pdf")
Common HTTP Request Headers
- Authorization: Sends authentication credentials (Bearer token, Basic auth, etc.)
- Content-Type: Defines the format of the request body (e.g., application/json)
- Accept: Specifies the expected response format (e.g., text/html, application/json)
- User-Agent: Identifies the client making the request (browser, API client, etc.)
- Referer: Indicates the page that linked to the current request
- Cookie: Sends cookies for session tracking
Frequently Asked Questions
What is an HTTP request header?
An HTTP request header is a piece of metadata sent by a client to a server when making an HTTP request, providing additional information about the request.
How can I use HTTP request headers in PDF rendering?
In IronPDF, you can use HTTP request headers by preparing them as a C# dictionary and assigning the dictionary to the HttpRequestHeaders property of the HTML to PDF renderer.
What are common HTTP request headers?
Common HTTP request headers include Authorization, Content-Type, Accept, User-Agent, Referer, and Cookie.
How do I download a library for use with HTTP request headers?
You can download IronPDF from NuGet by visiting the package page and following the instructions to include it in your project.
What is the purpose of the Authorization header?
The Authorization header is used to send authentication credentials, such as a Bearer token or Basic auth, to the server.
How do you specify the expected response format in HTTP headers?
The expected response format is specified using the Accept header, where you can define formats like text/html or application/json.
What is an example of using HTTP request headers in PDF rendering?
An example includes creating a dictionary of headers, such as Authorization and Content-Type, assigning them to the HttpRequestHeaders property, and rendering a URL to PDF using the RenderUrlAsPdf method in IronPDF.
Can the rendered PDF be exported in different formats?
Yes, IronPDF can save the rendered PDF as a file or export it as bytes, allowing you to choose the format that fits your needs.