1

new learner here. I usually test my GET api with Postman, where I can send a Get request with a JSON body.

I'm trying to do the same using Javascript to send a http GET request with a Body like how we can do in Postman. I've read the MDN on fetch and body but I still don't know how to apply it.

My javascript so far (in a React App) is

    const a = fetch(ApiUrl)
      .then(response => response.json())
      .then(data=>setState(data))

But I don't know where to insert my body property, my body is something like this:

{
    "StartDate": "2019-01-16T00:00:00",
    "EndDate": "2021-02-16T00:00:00",
    "FirstName": "Bart",
    "LastName": "Simpson",
    "Accounts": {
        "Savings": 1234,
        "Checkings": 4321,
        "Investments": 1234,
    }
}

Many thanks.

K Y
  • 191
  • 1
  • 8
  • @LaurentS. Thanks! The link is super helpful. This leads to a question I always want to ask but too afraid to ask for being too vague. If Get with a Body is not a good method to push in parameters for API to give a return a value, what would be a better method? Post? But Post sounds like it's when we use to change database? – K Y Apr 28 '21 at 22:24
  • 1
    I think [this article](https://evertpot.com/dropbox-post-api/) is interesting, but I never saw it implemented that way anywhere. I've always seen POST being used for that purpose. And I never managed to have the devs to agree that it was not just nitpicking and change it. On an existing codebase this might indeed turn out quite costly to change and retest, and getting business to pay for that is also never going to happen :-) – Laurent S. Apr 28 '21 at 22:33
  • And please, never refrain to ask such questions (be careful that they fit SO though, some other SE sites might be best suited depending on the question). I would love that more developers I work with ask such questions. – Laurent S. Apr 28 '21 at 22:38
  • Not sure who downvoted this, but this question doesn't deserve downvoting. It's only a very good question that already has an answer and therefore should be closed... – Laurent S. Apr 28 '21 at 22:40
  • @LaurentS. The blog post you shared is my answer. It's a very helpful article. Thank you. – K Y Apr 28 '21 at 22:55

0 Answers0