Adam Millerchip
Adam Millerchip

Reputation: 23147

Parsing and using the HTTP Range header with Spring

I'm writing a Spring controller that needs to accept the Range header, use the values there to request some data using a 3rd-party library, then return the result.

The Spring HttpRange class looks promising, specifically the parseRanges function, which says:

Parse the given, comma-separated string into a list of HttpRange objects.

This method can be used to parse an [sic] Range header.

However, this class doesn't provide any interface to extract the parsed values without knowing the "the total length of a representation".

Given I am handling a request which I'll forward to another API, I don't know the total length.

Is there a standard Spring way to inspect the Range header and extract the values without knowing the length? I want to avoid writing a custom parser if possible.

Upvotes: 3

Views: 1457

Answers (1)

mcacorner
mcacorner

Reputation: 1274

You can try this:

HttpHeaders rangeHeaders = new ServletServerHttpRequest(httpRequest).getHeaders();
List<HttpRange> ranges = rangeHeaders.getRange();

Refer: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html#getRange()

Upvotes: 0

Related Questions