1

I am working on the springboot application where I need to add a header to request API where they used org.springframework.http.HttpHeaders to set the Header values to the request.

I can see they used below code for setting the string value header.

HttpHeaders headers = new HttpHeaders();
    headers.set("correlationId", "12232324545x32321");

My requirement is to add a header named x-ms-documentdb-partitionkey and it expect the value as an array:

x-ms-documentdb-partitionkey = ["file1"]

How can set the above using using the HttpHeaders. I refer the below the Javadocs. I could not able to figure out the right way to do.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html

Thanks

Matt
  • 12,848
  • 2
  • 31
  • 53
Infinity
  • 484
  • 2
  • 8
  • 21
  • Have a look at this: https://stackoverflow.com/a/3097052/8668332 You should either add the header multiple times or concatenate the values. – Sebastian Heikamp Jul 05 '21 at 09:29

2 Answers2

1

Try to write [ and ] explicte in the value

HttpHeaders headers = new HttpHeaders();
    headers.set("x-ms-documentdb-partitionkey", "[\"file1\"]");
Ralph
  • 118,862
  • 56
  • 287
  • 383
1

HTTP headers are just name-value pairs (as strings). Consequently, you can just set this array in the required representation as the value. Don't forget to escape " properly.

HttpHeaders headers = new HttpHeaders();
headers.set("correlationId", "12232324545x32321");
headers.set("x-ms-documentdb-partitionkey", "[\"file1\"]");

System.out.println(headers);
[correlationId:"12232324545x32321", x-ms-documentdb-partitionkey:"["file1"]"]
Matt
  • 12,848
  • 2
  • 31
  • 53