Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hot to pass a Date in a header using Postman?

I have a POST method which accept a Header variable, which is mapped to java.time.OffsetDateTime in the Java endpoint. But when I try to pass the date for the Header variable in Postman in UTC format like "2019-09-18T20:15:32.162Z" or in timestamp like 1568835908, I get

"status": 400,
"error": "Bad Request",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.OffsetDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @org.springframework.web.bind.annotation.RequestHeader java.time.OffsetDateTime] for value '1568835908'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [1568835908]

I know I am passing the Date in wrong way in Postman header. What is the right way?

like image 628
Nicolas Avatar asked Oct 23 '25 18:10

Nicolas


2 Answers

RFC 1123 & RFC 822

According to this post, linked to this JavaScript library call, the Postman tool is expecting the date-time value in the obsolete format used in early Internet protocols. Strings looked like this:

Wed, 14 Jun 2017 07:00:00 GMT

That legacy format was defined in RFC 1123 and RFC 822. Be aware that modern protocols nowadays adopt ISO 8601 instead, including the java.time classes.

Fortunately, the DateTimeFormatter class has a constant, predefining this format: DateTimeFormatter.RFC_1123_DATE_TIME.

Instant instant = Instant.parse( "2019-09-18T20:15:32.162Z" ) ;
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
String output = odt.format( DateTimeFormatter.RFC_1123_DATE_TIME ) ;

See this code run live at IdeOne.com.

output: Wed, 18 Sep 2019 20:15:32 GMT

And parsing.

OffsetDateTime odt2 = OffsetDateTime.parse( output , DateTimeFormatter.RFC_1123_DATE_TIME ) ;

odt2.toString(): 2019-09-18T20:15:32Z

Again, this format is terrible and should be avoided. It assumes English, and assumes certain cultural norms for abbreviation/capitalization and such. It is difficult to parse by machine. Avoid this format where possible, using ISO 8601 formats instead to communicate date-time values as text. But if you must interoperate with legacy code not yet updated to modern protocols and formats, you can generate and parse such text using that predefined formatter.

like image 127
Basil Bourque Avatar answered Oct 26 '25 07:10

Basil Bourque


Resolved the issue by adding @DateTimeFormat from org.springframework.format.annotation.DateTimeFormat i.e. @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) in method argument declaration.

like image 37
Nicolas Avatar answered Oct 26 '25 06:10

Nicolas