Closed risonsimon closed 8 years ago
Hi @risonsimon,
It should return the full object + the id. Usually, it means that the endpoint didn't receive the object and therefore only returns the id.
You can test a POST on http://jsonplaceholder.typicode.com Here's how to make it https://github.com/typicode/jsonplaceholder#creating-a-resource
If you want, you can copy/paste your request code, I'll have a look.
I'm using fakerjs to generate data. This is for one resource.
users: _.times(10, function (id){
return {
id: id + 1,
email: faker.internet.email(),
first_name: faker.name.firstName(),
last_name: faker.name.lastName(),
}
})
When I post to http://localhost:3000/users
with following data:
{
"email": "risonsimon@hotmail.com",
}
I only get the id
value in the response.
I'm using json-server version 0.8.2
Sorry, wasn't clear. I meant the JS code that is doing the POST request. For example, with jQuery you should be able to create a user like this:
$.ajax('http://localhost:3000/users', {
method: 'POST',
data: {
"email": "risonsimon@hotmail.com"
}
}).then(function(data) {
console.log(data); // should return { id: 11, email: 'risonsimon@hotmail.com' }
})
I'm using fetch.
I've found the issue. I was not setting the header to application/json
. Because of this, the media type was being set as text
. Once I added the following to my header, it started to return the full object. Btw, thanks for this awesome product. :)
'Accept': 'application/json',
'Content-Type': 'application/json'
Here's the code I'm using.
fetch(`${URLPATH}`, {
method: 'post',
headers: {
'Authorization': `Token ${token}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
first_name: first_name,
last_name: last_name,
email_id: email
})
})
.then(response => {
console.log(response);
})
You're welcome. Glad it's solved :)
Thanks @risonsimon your tip worked for me too.
Hi,
Is it possible to return the full object that was created at a post endpoint rather than just the id?
For example, when I post to an endpoint with some json data, the item gets created and the id of the new item is returned. Is it possible to return the whole object, along with the data that was sent?