1

I am consuming a third party API which expects an HTTP header value as below (using the format below).

Date: 2017-10-15T14:25:21Z

When I try to add the header as below, I am getting invalid date time format error (when the client inject the header during runtime)

client.DefaultRequestHeaders.Add("Date", "2017-10-15T14:25:21Z");

So I change the code above to as below

client.DefaultRequestHeaders.Date = DateTime.UtcNow;

However, the API throws back an exception "Hmac timestamp 2019-01-02 is not a valid ISO8601 dateTime"

How do I pass the expected date time format in the HTTP request header?

EDIT

- I know how to get a date string in a specified format. What I am asking here is how do I pass a UTC date object to HTTP header as below with a specific format. I also realise that datetime object cant have a format in it.

  **client.DefaultRequestHeaders.Date = DateTime.UtcNow**
ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Alan B
  • 2,219
  • 4
  • 32
  • 62
  • 1
    Possible duplicate of [Given a DateTime object, how do I get an ISO 8601 date in string format?](https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – Corey Jan 08 '19 at 06:35
  • @Corey, why do you think it is a duplicate? It's not about converting a date into a specified format but more on how to pass the correct format of the date into HTTP request header – Alan B Jan 08 '19 at 22:50
  • Retracted, since you seem to be having a different issue. – Corey Jan 08 '19 at 23:21

1 Answers1

0

Do that like this

DateTime dt=Convert.ToDateTime("2019-01-02");           
string yourdate = dt.ToString("yyyy-MM-ddTHH:mm:ssZ");           
Console.WriteLine(yourdate);

Working example

enter image description here

ArunPratap
  • 4,816
  • 7
  • 25
  • 43