From 88bd7242b965d80beba56f0e74bc0e5e7792be09 Mon Sep 17 00:00:00 2001 From: Ghada Gharbi Date: Fri, 28 Jun 2019 19:20:28 +0200 Subject: [PATCH] update to support content-length header --- server.js | 16 +++++++++++----- tutorials/REST API.md | 11 +++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index 6af8562..d78a5db 100644 --- a/server.js +++ b/server.js @@ -53,7 +53,8 @@ app.use(bodyParser.urlencoded({extended: true})); var checker = function (req, res, next) { var headerAccept=req.header('Accept'); const headerRegExp=RegExp('.*/.*'); - var headerContentType=req.header('Content-Type') + var headerContentType=req.header('Content-Type'); + var headerContentLength=req.header('Content-Length'); if (req.method == "GET") { if ((headerRegExp.test(headerAccept)) || (headerAccept === 'undefined')) { next(); @@ -62,11 +63,16 @@ var checker = function (req, res, next) { res.send('Please Check Accept request header'); } } else if ((req.method == "POST") || (req.method == "PATCH")) { - if ((headerContentType == 'application/ld+json') || (headerContentType == 'application/json')) { - next(); + if (headerContentLength === undefined) { + res.status(411); + res.send('Please Check Content-Length request header'); } else { - res.status(400); - res.send('Please Check Content-Type request header'); + if ((headerContentType == 'application/ld+json') || (headerContentType == 'application/json')) { + next(); + } else { + res.status(400); + res.send('Please Check Content-Type request header'); + } } } else if (req.method == "DELETE") { next(); diff --git a/tutorials/REST API.md b/tutorials/REST API.md index 4839ef3..17a7784 100644 --- a/tutorials/REST API.md +++ b/tutorials/REST API.md @@ -41,6 +41,7 @@ The following HTTP request creates a csourceRegistration resource which describe POST /ngsi-ld/v1/csourceRegistrations/ HTTP/1.1 Host: localhost:3000 Content-Type: application/ld+json + Content-Length: 1805 Accept: application/ld+json { @@ -136,6 +137,7 @@ The following HTTP request creates a vehicle entity with the id *urn:ngsi-ld:Veh POST /ngsi-ld/v1/entities/ HTTP/1.1 Host: localhost:3000 Content-Type: application/ld+json + Content-Length: 683 Accept: application/ld+json { @@ -181,6 +183,7 @@ The following HTTP request creates a offStreetParking entity with the id *urn:ng POST /ngsi-ld/v1/entities/ HTTP/1.1 Host: localhost:3000 Content-Type: application/ld+json + Content-Length: 751 Accept: application/ld+json { @@ -227,6 +230,7 @@ If the creation was successful, the response with HTTP return code *201 Created* POST /ngsi-ld/v1/entities/ HTTP/1.1 Host: localhost:3000 Content-Type: application/ld+json + Content-Length: 511 Accept: application/ld+json { @@ -444,6 +448,7 @@ This example shows how to append a new attribute (*speed* property) to an existi POST /ngsi-ld/v1/entities/urn:ngsi-ld:Vehicle:A4567/attrs HTTP/1.1 Host: localhost:3000 Content-Type: application/ld+json + Content-Length: 198 { "speed": { @@ -470,6 +475,11 @@ changes, a new notification (including the new speed value) will be transmitted **HTTP Request** + POST /ngsi-ld/v1/subscriptions HTTP/1.1 + Host: localhost:3000 + Content-Type: application/ld+json + Content-Length: 664 + { "id": "urn:ngsi-ld:Subscription:mySubscription", "type": "Subscription", @@ -508,6 +518,7 @@ changes, a new notification (including the new speed value) will be transmitted PATCH http://127.0.0.1:3000/ngsi-ld/v1/entities/urn:ngsi-ld:Vehicle:A4567/attrs/speed Host: localhost:3000 Content-Type: application/ld+json + Content-Length: 241 { "id": "urn:ngsi-ld:Vehicle:A4567",