Skip to content

Commit

Permalink
update to support content-length header
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghada Gharbi committed Jun 28, 2019
1 parent 984c04c commit 88bd724
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
16 changes: 11 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions tutorials/REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

{
Expand Down Expand Up @@ -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

{
Expand Down Expand Up @@ -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

{
Expand Down Expand Up @@ -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

{
Expand Down Expand Up @@ -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": {
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 88bd724

Please sign in to comment.