1

today I tried to send a post-Request that looks like that. http://host/start?time=123456789 using the following code:

    const url = `/start`;
    const myParams = new HttpParams().set('time', toTimestamp(time).toString());
    return this.http.post(url, { headers: this.getHttpHeaders(), params: myParams }));

However The request was send without the parameter http://host/start. The Parameter was send inside the body of the request. The API does not accept that and I am not allowed to change the API. How could I change my code to include the parameter inside the URL?

Thanks for your help.

2
  • You can pass with url '/start?time=123456' Commented Jan 17, 2019 at 16:21
  • If you are only willing to send a post request like you mentioned you can crate a url string by appending the param and param value in the url and then calling post. but POST does for a purpose send data in the body Commented Jan 17, 2019 at 16:33

2 Answers 2

1

As mentioned by Sanyam, the post request looks like...

http.post(url, data, httpOptions)

So in your request you are missing the actual body part... since this is a POST request. Since you have no control of the API, you can add null for the body part.

return this.http.post(url, null, { headers: this.getHttpHeaders(), params: myParams }));
Sign up to request clarification or add additional context in comments.

Comments

0

When you do

return this.http.post(url, { headers: this.getHttpHeaders(), params: myParams }));

You are sending parameters in the request body which is what is expected here

  1. GET sends data in URL to server for getting data from a resource
  2. POST sends params in body to the server for update/creation of resource

Follow this order in your request :

 http.post(url, data, httpOptions)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.