构建API时首选哪种方法 [英] Which method is prefer when building API

查看:61
本文介绍了构建API时首选哪种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用快速框架构建API,但目前我需要选择请求/响应方法

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

有一种使用JSON的方法,因此某人可以发送带有JSON数据的发帖请求,但这种方式不能通过HTML表单发出

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

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

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

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

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.

您使用的是Express,因此要解析请求正文,您将使用正文解析器.参见:

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

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

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)

使用JSON编码的示例请求:

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:

这是一个接受JSON的服务器:

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:

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

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?

作为一名开发人员,我个人更喜欢使用JSON,但我可以理解是否需要使用表单编码.借助Express,可以轻松同时支持这两者,因此您不必只选择其中一个.

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.

但是,如果您出于某种原因只选择一个,请与您的开发人员联系.这些是您API的使用者,所以他们是唯一可以告诉您更好"的人.

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

这篇关于构建API时首选哪种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆