Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

javascript - json-server 404 on POST req to /{route}/{id}

I'm trying to send a post request to update an existing record while using a json-server mock API.

I can send GET requests to specific ids and POST reqs to generic endpoints to create new records, but POSTs to specific ids returns a 404. How can it be "Not Found" if I can find it just fine with a GET?

All the questions about it on SO/Github seem to be irrelevant -- mostly coding typos that don't apply. This one seems relevant but is as yet unresolved.

Here's my failing request:

await fetch(`http://localhost:8080/profile/1`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    // ...bunch of new data
  }),
})
  .then((res) => res.json())
  .catch((err) => console.log(err));

Successful POST to generic endpoint:

const newProfile = await fetch(`http://localhost:8080/profile`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    // ...bunch of original data
  }),
})
  .then((res) => res.json())
  .catch((err) => console.log(err));

Successful GET to specific endpoint

const profileData = await fetch(
  `http://localhost:8080/profile/${match.params.userId}`
)
  .then((res) => res.json())
  .catch((err) => console.log(err));
question from:https://stackoverflow.com/questions/65939557/json-server-404-on-post-req-to-route-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...