构建API时,哪种方法更适合?[英] Which method is prefer when building API

本文是小编为大家收集整理的关于构建API时,哪种方法更适合?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在构建具有Express Framework的API,但目前我需要选择请求/响应方法

有一种使用JSON的方法,因此有人可以发送JSON数据的帖子请求,但是以这种方式无法通过HTML表单

提出请求

但是,如果我使用可以通过html表单请求的方法,那么其他开发人员可以使用此API来构建自己的应用程序或客户端

所以我的问题是,在构建API时,哪种方法是接受JSON请求或正常请求,如果我的API是为其他开发人员制作的,哪个方法更好?

推荐答案

首先让您提供一些背景,然后我继续进行特定答案.

您正在使用Express,以解析您使用Body-Parser的请求主体.请参阅:

  • https://www.npmjs.com/package/package/parkage/body-parser

也许最好查看一些可以通过使用JSON和数据以HTML表单的格式发送请求来测试的示例.

使用表单编码的示例请求:

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

(这是一个单个命令,可以放在没有后斜线的一行中)

使用JSON编码的示例请求:

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

(同样,这也是一个命令 - 在多行上戴多行以进行可读性)

现在,可以响应这些请求的服务器代码:

这是接受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/'));

这是一台接受形式数据的服务器:

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/'));

这是一台接受两者的服务器:

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/'));

您可以看到,支持这两种样式是很重要的,而您不需要选择其中一种.

现在,对于您的特定问题:

所以我的问题是,在构建API时,哪种方法是接受JSON请求或正常请求,如果我的API是为其他开发人员制作的,哪个方法更好?

作为开发人员,我个人更喜欢使用JSON,但我可以理解需要在需要的情况下使用该表单.使用Express,同时很容易支持两者,因此您不必只选择一个.

但是,如果您仅出于某种原因选择一个,请与您的开发人员交谈.这些是您的API的消费者,因此他们是唯一能告诉您"更好"的人.

本文地址:https://www.itbaoku.cn/post/1937812.html

问题描述

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?

推荐答案

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".