pizza
pizza

Reputation: 742

Can I use Post method to fetch data?

I have done some reading regarding GET and POST methods and know that GET method should be used when fetching data where the parameters are in URL. Whereas POST in general should be used to store data with data in the body.

But, I have an API that takes JSON as input. So I think I need to use POST method but what bothers me is that I won't make any changes to backend. I just need to fetch data according to the parameters in the JSON.

Would it be a bad practice to use POST method just to accept JSON even though no changes will be made? If so, what other approaches can I take? Thank you

Upvotes: 0

Views: 4221

Answers (2)

idembele70
idembele70

Reputation: 139

This is how I use fetch to make a post with json data.

const data = {name : "toto", age : "12"}

fetch(url, {
      method: "POST",
      headers: { "content-type": "application/json" },
      body
    }).then(
     console.log(data added !) 
)

Upvotes: 0

fooiey
fooiey

Reputation: 1600

Yes, you can add JSON data in the body. I'm not sure what language you are using, but for example in the requests module in Python, you can add some data to the Get request as shown in here.

Upvotes: 1

Related Questions