-1

I want to pass date in angular 8 $http Get request. Please suggest me that what is the correct way to pass date.

i want to do something like this

public getCalendarData(Date): any {
        let myResponse = this.http.get(`${environment.BASE_URL}/getCalendarData` + '/' + new Date().toUTCString(), {
            headers: new HttpHeaders().set('SESSION-TOKEN', localStorage.getItem('jwt_token'))
        });
        console.log(myResponse);
        return myResponse;
    }

I have to integrate this API https://api/getCalendarData/20-10-2020 with angular but I don't know how to pass date in angular 8 $http Get request,

Liam
  • 27,717
  • 28
  • 128
  • 190
vidya
  • 11
  • 2
  • Does this answer your question? [Angular HttpClient Get method with body](https://stackoverflow.com/questions/54164149/angular-httpclient-get-method-with-body) – jonrsharpe Oct 22 '20 at 09:01
  • The date could also be a query paremeter https://stackoverflow.com/questions/34475523/how-to-pass-url-arguments-query-string-to-a-http-request-on-angular – MoxxiManagarm Oct 22 '20 at 09:02
  • Does this answer your question? [How to pass the date as URL parameter](https://stackoverflow.com/questions/28323895/how-to-pass-the-date-as-url-parameter) – Liam Oct 22 '20 at 10:36

1 Answers1

1

This is what your url would look like with your current implementation:

<BASE>/getCalendarData/Thu, 22 Oct 2020 09:18:59 GMT

Certainly not ideal as the url contains whitespace and a comma.

I would rather propose to pass a timestamp to your backend:

const timestamp = Date.parse(new Date())

Then your url would look like this:

<BASE>/getCalendarData/1603358349000

As @MoxxiManagarm mentioned, another option would be to send the date in a mandatory query parameter, this would then look like this:

<BASE>/getCalendarData?timestamp=1603358349000

I think this is nicer, because the path variable usage suggests that there is a concrete usage for each unique date - which there probably isn't.

Yet another option would be to send a POST request as it can contain a body, in which you could format the date in any way you want. This is obviously not REST conform, but might be a pragmatic solution for now.

code-gorilla
  • 2,231
  • 1
  • 6
  • 21