-1

I'm building a API with express framework but currently I need to choose the request/response method

There is a method using JSON so someone can send a post request for example with JSON data but in this way the request can't be make by HTML form

But if I use the method that can be request by HTML form can this API be used by other developers for building their own application or client

So my question is which method is better when building a API is it accepting JSON request or normal request, which one is better if my API is made for other developers?

Andrew Young
  • 55
  • 10
  • It's best to keep to the posting rules: https://stackoverflow.com/help/on-topic – Peter Grainger Jul 13 '17 at 07:10
  • It depends entirely upon what works best for your target developer audience and only you can know that. If your audience wants to post directly from HTML forms with no intervening Javascript, then you need to support that type of incoming data. If your developers are only making requests from Javascript, the JSON is probably a more natural data format for the incoming data. You choose which fits your audience the best. We don't know anything about your intended developer and/or what they are trying to do. – jfriend00 Jul 13 '17 at 07:18

1 Answers1

0

First let be give you some background and then I'll proceed to specific answer.

You're using Express so to parse the request body you'll use the body-parser. See:

Maybe it's best to look at some examples that you can test by sending requests using both JSON and data in the format coming from HTML forms.

Example request using form encoding:

curl -X POST localhost:4443 \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'a=X&b=Y'

(this is a single command that can be put in one line without the backslashes)

Example request using JSON encoding:

curl -X POST localhost:4443 \
  -H 'Content-type: application/json' \
  -d '{"a":"X","b":"Y"}'

(again this is also a single command - put on multiple lines for readability)

Now, the server code that can respond to those requests:

This is a server that accepts JSON:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());

app.post('/', (req, res) => {
  console.log(req.body);
  res.json({ ok: true });
});

app.listen(4443, () => console.log('Listening on http://localhost:4443/'));

This is a server that accepts form data:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.post('/', (req, res) => {
  console.log(req.body);
  res.json({ ok: true });
});

app.listen(4443, () => console.log('Listening on http://localhost:4443/'));

This is a server that accepts both:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/', (req, res) => {
  console.log(req.body);
  res.json({ ok: true });
});

app.listen(4443, () => console.log('Listening on http://localhost:4443/'));

As you can see it is trivial to support both styles and you don't need to choose only one of them.

Now, to your specific question:

So my question is which method is better when building a API is it accepting JSON request or normal request, which one is better if my API is made for other developers?

As a developer I personally prefer using JSON but I can understand the need to use the form encoding if it's needed. With Express it's very easy to support both at the same time so you don't have to choose just one.

But if you want to choose only one for some reason then talk to your developers. Those are the consumers of your API so they are the only people who can tell you what's "better".

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Then will it be a situation that someone send a request with both form data and JSON is it possible? – Andrew Young Jul 13 '17 at 12:35
  • @AndrewYoung In any given request there can only be either form data or JSON because the request can only have one Content Type. – rsp Jul 13 '17 at 12:54