4

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?

Helen
  • 87,344
  • 17
  • 243
  • 314
Nicolas
  • 554
  • 2
  • 11
  • 27
  • maybe you can use this : https://stackoverflow.com/questions/47355150/how-do-i-format-timestamp-as-mm-dd-yyyy-in-postman – Syrup72 Sep 18 '19 at 20:23
  • can you show your server side method? I supose that you should send something else, for example a JSON and JSON does not have dates, but representation of dates as a string or number and then you are converting It to Date on server side – vmrvictor Sep 18 '19 at 20:42
  • Possible duplicate of [Java Spring RESTFull API Request With Dates](https://stackoverflow.com/questions/34185986/java-spring-restfull-api-request-with-dates) – vmrvictor Sep 18 '19 at 20:51
  • @vmrvictor I suspect this Question is really about the terrible old format used by email and other early internet protocols, defined in RFC 1123 & RFC 822. So that Question you linked would not be the original of a duplicate here. The Question you linked is about the “basic” variation of ISO 8601 format. – Basil Bourque Sep 18 '19 at 22:14
  • thanks, I was suposing, for this reason I repeat my question, can you show your server side method – vmrvictor Sep 19 '19 at 04:50
  • For passing Date in the Header, I tried using moment i.e. installing moment and specifying the script `var moment = require('moment') pm.globals.set("timestamp", moment().format("MM/DD/YYYY"));` in pre-request Script tab in Postman and sending the request, throws the error message "There was an error in evaluating the Pre-request Script: Cannot find module moment". am I missing some config for moment with postman? – Nicolas Sep 19 '19 at 14:59

2 Answers2

1

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

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

Nicolas
  • 554
  • 2
  • 11
  • 27