0

I am new to using json-server and json-schema-faker. I can get it to generate records but when I post to the endpoint, the only thing that it posts to the database is an object with id, why doesn't it have the other fields (i.e name, pro1, pro2 etc). Can anyone help?

Below is my schema:

    export const schema = {
    "type": "object",
    "properties": {
        "tests": {
            "type": "array",
            "minItems": 1,
            "maxItems": 6,
            "items": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "number",
                        "unique": true,
                        "minimum": 1
                    },
                    "name": {
                        "type": "string"
                    },
                    "pro1": {
                        "type": "integer",
                        "minimum": 0,
                        "maximum": 100
                    },
                    "pro2": {
                        "format": "date-time",
                        "type": "string"
                    },
                    "pro3": {
                        "type": "boolean"
                    },
                    "pro4": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 10
                    },
                    "pro5": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 10
                    }
                },
                "required": ["id", "name", "pro1", "pro2", "pro3", "pro4", "pro5"]
            }
        }
    },
    "required": ["tests"]
};

And my generate file:

import jsf from 'json-schema-faker';
import {schema} from './mockDataSchema';
import fs from 'fs';
import chalk from 'chalk';

const json = JSON.stringify(jsf(schema));

fs.writeFile("./src/api/db.json", json, function(err){
    if (err){
        return console.log(chalk.red(err));
    }else{
        return console.log(chalk.green("Mock Data Generated"));
    }
});
user3214545
  • 2,103
  • 3
  • 20
  • 26

1 Answers1

1

In Header-s part of POST-request, must have header : "Content-Type: application/json"

In Body part ('raw' object) of POST-request, write object, that have to be post-ed to server like this :

{
    "id": 1,
    "name": "John Doe",
    "email": "John.Doe@mailserver.com"
}

For example : with POSTMAN (http client application), the POST request is like this :

POST /users HTTP/1.1
Host: 127.0.0.1:3001
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: some postman token

{
    "id": 1,
    "name": "John Doe",
    "email": "John.Doe@mailserver.com"
}
Ted
  • 843
  • 10
  • 13