尝试使用express4发布或放置时出现JSON格式错误

尝试使用express4发布或放置时出现JSON格式错误,json,sql-server,rest,express,azure-sql-database,Json,Sql Server,Rest,Express,Azure Sql Database,已找到我尝试使用的sql server示例“Todo”应用程序。它应该用很少的代码行将SQL Server数据库转换为REST API 我无法发布或放置。它给出了一个错误 message: "JSON text is not properly formatted. Unexpected character 'o' is found at position 1.", code: 'EREQUEST', number: 13609, state: 4, class: 16, serverName:

已找到我尝试使用的sql server示例“Todo”应用程序。它应该用很少的代码行将SQL Server数据库转换为REST API

我无法发布或放置。它给出了一个错误

message: "JSON text is not properly formatted. Unexpected character 'o' is found at position 1.",
code: 'EREQUEST',
number: 13609,
state: 4,
class: 16,
serverName: 'SERVER',
procName: 'createTodo',
lineNumber: 4
GET、GET by id或DELETE没有问题。获取和删除与SQL Server 2017 Developer、Express和Azure SQL数据库完美配合

从SQL Server或Azure中运行存储过程本身时没有错误。只有在尝试从命令行POST或PUT时,才会出现错误:

curl -X "POST" "http://localhost:3000/todo"
-i
-H 'Content-Type: application/json'
-d '{"title": "test_title", "description": "test_description", "completed": false, "dueDate": "2014-12-14T00:00:00.000Z"}'
没有发现JSON文本本身有任何问题。在位置1处未看到任何字符“o”。我尝试了nvarchar、varchar的所有变体,在JSON字符串的开头使用了N‘vs’,将描述改为descripp,认为它可能是SQL Server 2017下的保留字,但似乎没有任何效果

使用Azure SQL数据库时出现的错误消息略有不同:

JSON text is not properly formatted. Unexpected character \'o\' is found at position 1.
app.js

var express = require('express');
var config = require('config');
var bodyParser = require('body-parser');
var tediousExpress = require('express4-tedious');

var app = express();
app.use(function (req, res, next) {
    req.sql = tediousExpress(config.get('connection'));
    next();
});

app.use(bodyParser.text()); 
app.use('/todo', require('./routes/todo'));

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found: '+ req.method + ":" + req.originalUrl);
    err.status = 404;
    next(err);
});
app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
    console.log('Express server listening on port ' + server.address().port);
});

module.exports = app;
todo.js

var router = require('express').Router();
var TYPES = require('tedious').TYPES;

/* GET task listing. */
router.get('/', function (req, res) {

    req.sql("select * from todo for json path")
        .into(res, '[]');

});

/* GET single task. */
router.get('/:id', function (req, res) {

    req.sql("select * from todo where id = @id for json path, without_array_wrapper")
        .param('id', req.params.id, TYPES.Int)
        .into(res, '{}');

});

/* POST create task. */
router.post('/', function (req, res) {

    req.sql("exec createTodo @todo")
        .param('todo', req.body, TYPES.NVarChar)
        .exec(res);

});

/* PUT update task. */
router.put('/:id', function (req, res) {

    req.sql("exec updateTodo @id, @todo")
        .param('id', req.params.id, TYPES.Int)
        .param('todo', req.body, TYPES.NVarChar)
        .exec(res);

});

/* DELETE single task. */
router.delete('/:id', function (req, res) {

    req.sql("delete from todo where id = @id")
        .param('id', req.params.id, TYPES.Int)
        .exec(res);

});

module.exports = router;
default.json

{
    "connection":{
        "server"  : "<name>.database.windows.net",
        "userName": "username",
        "password": "password",
        "options": { "encrypt": "true", "database": "<azure-database-name>" }
    }
}

我认为有一个小错误。您正在使用
body parser.text()
,但将数据作为JSON发送。更改卷曲请求如下:

 curl -X "POST" "http://localhost:3000/todo" -i -H 'Content-Type: text/plain' -d '{"title": "test_title", "description": "test_description", "completed": false, "dueDate": "2014-12-14T00:00:00.000Z"}'
此外,您还可以使用
Postman
测试API,这更方便

确保传递给过程的JSON字符串应该是准确的。您可以通过运行以下脚本来验证过程的输入:

DECLARE @json NVARCHAR(MAX)

SET @json='{"name":"John","surname":"Doe","age":45,"skills":["SQL","C#","MVC"]}';

SELECT *
FROM OPENJSON(@json);

这么小的细节。我之前一直在使用Postman,试图让get请求生效。应该坚持下去。发生了:)
DECLARE @json NVARCHAR(MAX)

SET @json='{"name":"John","surname":"Doe","age":45,"skills":["SQL","C#","MVC"]}';

SELECT *
FROM OPENJSON(@json);