The difference between the use of fetch get method and post method

Today, when learning fetch, to learn get and post, in the course of the experiment, it was found that get could successfully obtain the data in api, but post did not know where it went wrong. There is no problem with the get method as follows

fetch("https://search.heweather.com/find?location=&key=bc08513d63c749aab3761f77d74fe820",{
      method:"GET"
    }) // Promise
    .then((res)=>{
      return res.json();
    })
    .then((res)=>{
      console.log(res) // res
    })

but the post method does not know what is wrong. Please point out

const content = {location:"",key:"bc08513d63c749aab3761f77d74fe820"} 
fetch("https://search.heweather.com/find",{
      method:"POST",
      body:JSON.stringify(content)
    }) // Promise
    .then((res)=>{
      return res.json();
    })
    .then((res)=>{
      console.log(res) // res
    })
    // invalid key

or can this API be called only in GET mode? For beginners to learn the knowledge of API, please give us a detailed explanation. Thank you

Jul.11,2021

the back-end of this API is not well designed. It should be reported directly to 405 method not allowed.. The back end of this API accepts both GET and POST, but the parameters only look at the QueryString section and do not check request body, so it is a bad example. You just need to change the method of your GET example to POST, to keep the key parameter passed in queryString, and the API will be returned correctly.

by the way, both GET and POST can take request body. Both can add the QueryString parameter after the question mark in the url path, but according to the semantic GET request, there is generally no request body.


you are all get


body:content

Content-type

Menu