Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put multiple values into one http header entry

Tags:

asp.net-core

Example in C# client

var c = new HttpClient();
c.DefaultRequestHeaders.Add("X-MyHeader", "123,abc"); // What delimiter? , ; & are not working.

In the Server

var Vals = Request.Headers["X-MyHeader"];

Since the Headers returns a StringValues object, may I know what delimiter shall I use at the client side, so that the string can be automatically parsed as '123' and 'abc' separately?

like image 450
s k Avatar asked May 28 '26 14:05

s k


1 Answers

Let the HttpHeaders collection handle the separators.

The Add() method can be called multiple times with a single value or with a list of values.

The HttpHeaders collection doesn't behave the same as a Dictionary<string, string>. One key can contain multiple values. They are send separated by comma's. See:

https://learn.microsoft.com/en-us/dotnet/api/system.net.http.headers.httpheaders.add?view=net-7.0

The list can be added in one call with the Add(String, IEnumerable<String>) overload, if you have the strings in a enumerable collection of some sort.

like image 141
Menno van Lavieren Avatar answered May 31 '26 18:05

Menno van Lavieren