The issue is the form.
Here's the working code:
<html>
<head>
<meta http-equiv="Content-Type" charset="UTF-8">
</head>
<body>
<h1>Please type in new item data:</h1><br>
<label class="add_label"><b> userId: </b></label>
<input type="number" class="add_input" id="new_userId" name="new_userId" value="">
<br>
<label class="add_label"><b> title: </b></label>
<input type="text" class="add_input" id="new_title" name="new_title" value="">
<br>
<label class="add_label"><b> body: </b></label>
<input type="text" class="add_input" id="new_body" name="new_body" value="">
<button id="add_btn2" onclick="New()">Submit</button>
<script>
function New() {
var userid = document.getElementById("new_userId").value;
var new_title = document.getElementById("new_title").value;
var new_body = document.getElementById("new_body").value;
console.log("Input Data: " + userid + " " + new_title + " " + new_body);
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: new_title,
body: new_body,
userId: userid
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => {
console.log('response: ' + JSON.stringify(json));
})
}
</script>
</body>
</html>
Check the console to see the response
Answer from saintlyzero on Stack OverflowJSONPlaceholder
jsonplaceholder.typicode.com
JSONPlaceholder - Free Fake REST API
JSONPlaceholder comes with a set of 6 common resources: Note: resources have relations. For example: posts have many comments, albums have many photos, ...
JSONPlaceholder
jsonplaceholder.typicode.com โบ guide
JSONPlaceholder - Guide
fetch('https://jsonplaceholder.typicode.com/posts', { method: 'POST', body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1, }), headers: { 'Content-type': 'application/json; charset=UTF-8', }, }) .then((response) => response.json()) .then((json) => console.log(json));
Videos
06:08
How to Make a POST Request to JSONPlaceholder API in ...
11:26
Using JSONPlaceholder Fake Rest API with Fetch in Javascript - YouTube
09:40
Javascript HTTP Post Request Fetch API Example | JSONPlaceholder ...
21:01
Create a Fake REST API with JSON-Server - YouTube
15:32
SwiftUI Tutorial 17: Posting JSON data to server - YouTube
06:49
Online fake REST API - YouTube
Top answer 1 of 2
4
The issue is the form.
Here's the working code:
<html>
<head>
<meta http-equiv="Content-Type" charset="UTF-8">
</head>
<body>
<h1>Please type in new item data:</h1><br>
<label class="add_label"><b> userId: </b></label>
<input type="number" class="add_input" id="new_userId" name="new_userId" value="">
<br>
<label class="add_label"><b> title: </b></label>
<input type="text" class="add_input" id="new_title" name="new_title" value="">
<br>
<label class="add_label"><b> body: </b></label>
<input type="text" class="add_input" id="new_body" name="new_body" value="">
<button id="add_btn2" onclick="New()">Submit</button>
<script>
function New() {
var userid = document.getElementById("new_userId").value;
var new_title = document.getElementById("new_title").value;
var new_body = document.getElementById("new_body").value;
console.log("Input Data: " + userid + " " + new_title + " " + new_body);
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: new_title,
body: new_body,
userId: userid
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => {
console.log('response: ' + JSON.stringify(json));
})
}
</script>
</body>
</html>
Check the console to see the response
2 of 2
2
First you need to prevent the form from submitting, if not it will refresh the page. You can try this:
In Form
... <button id = "add_btn2" onclick = "New(event)" type = "submit">Submit</button> ...In function
function New(e) { e.preventDefault() ...As stated here JSONPlaceholder Guide, the resource will not be really created. Successful request will return a
jsonobject based on form-data you've sent. Just check your browser log, check if the responses just like what the documentation said.
GitHub
github.com โบ typicode โบ jsonplaceholder
GitHub - typicode/jsonplaceholder: A simple online fake REST API server ยท GitHub
JSONPlaceholder is a simple fake REST API for testing and prototyping. It's like an image placeholder but for web developers.
Starred by 5.2K users
Forked by 594 users
Languages ย HTML 68.1% | JavaScript 31.9%
Eviltester
apichallenges.eviltester.com โบ practice-sites โบ jsonplaceholder
JSON Placeholder - Simulator API - Example API
JSON Placeholder is an API that returns canned data. It also simulates the effects of update methods like POST, PUT, DELETE, PATCH by returning what would have happened if the request you made had taken effect.
JSON Placeholder
arnu515.github.io โบ jsonplaceholder-api-docs
JSON Placeholder
delDelete a post ยท Documentation Powered by ReDoc ยท Download OpenAPI specification:Download ยท Free fake API for testing and prototyping. JSON Placeholder's guide ยท 200 ยท All went well ยท get/posts ยท JSON Placeholder ยท https://jsonplaceholder.typicode.com/posts ยท
JSONPlaceholder
jsonplaceholder.org โบ home
JSONPlaceholder | JSON Placeholder - jsonplaceholder.org
April 9, 2023 - JSON Placeholder uses HTTP methods like POST, GET, PUT, PATCH, HEAD, and DELETE for interaction examples between clients and servers. While all these methods work, produce results, and are functional, the data is not permanently stored in JSONPlaceholder.
GitHub
github.com โบ typicode โบ jsonplaceholder โบ blob โบ master โบ templates โบ GUIDE.md
jsonplaceholder/templates/GUIDE.md at master ยท typicode/jsonplaceholder
In other words, if you try to access a post using 101 as an id, you'll get a 404 error. fetch('https://jsonplaceholder.typicode.com/posts/1', { method: 'PUT', body: JSON.stringify({ id: 1, title: 'foo', body: 'bar', userId: 1 }), headers: { "Content-type": "application/json; charset=UTF-8" } }) .then(response => response.json()) .then(json => console.log(json)) // Output { id: 1, title: 'foo', body: 'bar', userId: 1 }
Author ย typicode
Beeceptor
app.beeceptor.com โบ mock-server โบ json-placeholder
JSONPlaceholder APIs - Mock API
There are a variety of endpoints available and each provides JSON response for resources such as posts, users, comments, todos, etc. The mock server generates realistic random data on every invocation. Note: Don't use trailing slashes, and match the path exactly as shown. JSONPlaceholder APIs is your solution to frontend-first development!
ReqBin
reqbin.com โบ o0tzomnc
API request example to jsonplaceholder.typicode.com using the HTTP PUT method
An example of making an HTTP PUT request to the https://jsonplaceholder.typicode.com/posts/1 API endpoint.
YouTube
youtube.com โบ watch
JSON Placeholder API Tutorial | For Beginners - YouTube
In this Json Placeholder API Tutorial, I will show step by step how to use start using the JSON Placeholder api!Chapters:0:00 Intro0:07 Demo of JSON Placehol...
Published ย May 11, 2023 Views ย 6K
Postman
postman.com โบ lunar-eclipse-220985 โบ jsonplaceholder โบ request โบ 7k2jsze โบ post-request
Post request | JSONPlaceholder
Accelerate API development with Postman's all-in-one platform. Streamline collaboration and simplify the API lifecycle for faster, better results. Learn more.
Jsonplaceholder
jsonplaceholder.ir
jsonplaceholder | RTL fake REST/GraphQL API for testing and prototyping.
RTL fake REST/GraphQL API for testing and prototyping, based on {JSON}Placeholder. ... Try GET, POST or other HTTP methods on /posts, /comments, /photos, /todos or /users.
DummyJSON
dummyjson.com
DummyJSON - Free Fake REST API for Placeholder JSON Data
DummyJSON Server creates dynamic placeholders instantly, no more image hunting or complex setups. Just add the URL and you're good to go! ... Access real-life data, fully organized and ready to use, all in one place. ... Use the dropdown to get results for different resources. fetch(`https://dummyjson.com/ RESOURCE products carts users posts comments quotes recipes todos `) ... Need a REST API for custom JSON data?
Codeomelet
codeomelet.com โบ posts โบ javascript-xhr-using-jsonplaceholder-api
Javascript Xhr Using Jsonplaceholder Api | CodeOmelet
The response from JSONPlaceholder for this POST request will be a JSON object which looks like shown below. The id in the response will always be 101 and no real object is created at the API's end, this is just a fake response we get from JSONPlaceholder REST API with the response HTTP status as 201.
Jlstrater
jlstrater.github.io โบ spring-restdocs-public-api-example
Example API Guide for the JSON Placeholder API
A POST request will create new posts. $ curl 'http://jsonplaceholder.typicode.com/posts' -i -X POST -H 'Accept: */*' -H 'Content-Type: application/json; charset=UTF-8' -d '{"userId": 1, "id": 101, "title": "Test Example", "body": "Example Body"}'