skip to Main Content

In PHP I can fetch incoming JSON from let’s say an AJAX request by calling file_get_contents('php://input'), I’m now rebuilding my API in NodeJS (TypeScript) and am looking for an equivalent to fetch incoming JSON.

Various web searches did not deliver what I was looing for, so I hope to find help here.

2

Answers


  1. You can try to call the correct URL by using Axios. Following this link https://www.npmjs.com/package/axios for more.

    Login or Signup to reply.
  2. You can use bodyparser for express and receive data via post.

    let app = express();
    const bodyParser = require("body-parser");
    
    app.listen(port);
    console.log('API started port' + port);
    
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    
    app.post('/path', "maybe cors()", function(req, res){
    let data = req.body.data //data is the json you get
    }
    

    Thats the way i fetch json that gets send to the server. Hope you can make something with it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search