Dredd: Expected body Content-Type is application/json; charset=utf-8 but body is not a parseable JSON

Created on 23 Apr 2016  路  10Comments  路  Source: apiaryio/dredd

Hello.
I got from the Dredd test failed report because he could not parse the JSON but I think the response is correct and contains parseable JSON body.
I tried to validate response data with http://jsonlint.com/ and it passed. If I try call the API method by the Postman I get also valid JSON, so why Dredd can't parse it?

Full Dredd report:

> dredd

info: Configuration dredd.yml found, ignoring other arguments.
info: Using apiary reporter.
info: Starting server with command: node server.js
info: Waiting 3 seconds for server command to start...
info: Beginning Dredd testing...
info: Found Hookfiles: tests/dreddhooks.js
skip: GET /api/products/product
fail: POST /api/products/product duration: 150ms
~~~other skipped tests~~~ 
skip: GET /api/settings
info: Displaying failed tests...
fail: POST /api/products/product duration: 150ms
fail: body: Can't validate. Expected body Content-Type is application/json; charset=utf-8 but body is not a parseable JSON: Parse error on line 1:

^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'EOF'

request: 
body: 
{
  "name": "DREDD TEST | Lamborghini Huracan",
  "subheading": "DREDD TEST | Supersport car all wheel drive, engine 5.2L V10",
  "description": "DREDD TEST | The Lamborghini Hurac谩n is a sports car built by Lamborghini that replaces Lamborghini's sales leader and most produced car, the Gallardo.",
  "vendor_id": 1,
  "price_sale": 300000,
  "price_regular": 320000,
  "amount_available": 1616,
  "image_width": 2,
  "image_height": 1,
  "priority": 2,
  "publish_status": "active",
  "category_id": "business"
}
headers: 
    Content-Type: application/json
    User-Agent: Dredd/1.0.10 (Windows_NT 10.0.14295; x64)
    Content-Length: 466

uri: /api/products/product
method: POST


expected: 
headers: 
    Content-Type: application/json; charset=utf-8

body: 

statusCode: 201


actual: 
statusCode: 201
headers: 
    x-powered-by: Express
    content-type: application/json; charset=utf-8
    content-length: 638
    etag: W/"27e-ESB3wpmhXJgZSsrjly9wIw"
    set-cookie: connect.sid=s%3A4MLpXvSZSpKNhLyTXxXw8-gaZmcdUsYK.nrquq3V5bpw0dYzp4IbEfSZ1X7A7%2B%2BYVPBKkrhkPyC0; Path=/; HttpOnly
    date: Sat, 23 Apr 2016 21:24:56 GMT
    connection: close

body: 
{"product_id":32,"priority":2,"publish_status":"active","name":"DREDD TEST | Lamborghini Huracan","subheading":"DREDD TEST | Supersport car all wheel drive, engine 5.2L V10","description":"DREDD TEST | The Lamborghini Hurac谩n is a sports car built by Lamborghini that replaces Lamborghini's sales leader and most produced car, the Gallardo.","price_sale":300000,"price_regular":320000,"amount_available":0,"image_width":2,"image_height":1,"timestamp_create":"2016-04-23T21:24:56.000Z","timestamp_lastedit":"2016-04-23T21:24:56.000Z","vendor_id":1}



complete: 0 passing, 1 failing, 0 errors, 13 skipped, 14 total
complete: Tests took 1432ms
POST /api/products/product 201 105.337 ms - 638
complete: See results in Apiary at: https://app.apiary.io/~~~CENSORED~~~/tests/run/e79bd8df-08e9-43c6-a97a-829b27171222
info: Sending SIGTERM to the backend server
info: Backend server was killed

The server response is made by Express:

connection.query(sql_query, function (err, rows, fields) {
  (!err) {
    ...
    res.status(201).json(rows[0]);
    ...
  }
});

Apiblueprint file:

...

### Save New Product Item [POST /api/products/product]
+ Attributes (NewlyCreatedProductItem)
+ Request (application/json)
+ Response 201 (application/json; charset=utf-8)

...

# Data Structures

## NewlyCreatedProductItem (object)
+ name: Lamborghini Huracan (string, required)
+ subheading: Supersport car all wheel drive, engine 5.2L V10 (string, required)
+ description: The Lamborghini Hurac谩n is a sports car built by Lamborghini that replaces Lamborghini's sales leader and most produced car, the Gallardo. (string, required)
+ vendor_id: 1 (number, required)
+ price_sale: 300000 (number, required)
+ price_regular: 320000 (number)
+ amount_available: 1616 (number, required)
+ image_width: 2 (number, required)
+ image_height: 1 (number, required)
+ priority: 2 (number, required)
+ publish_status: active (string, required)
+ category_id: business (string, required)

...

Dreddhooks file:

before("Products > Product Item > Save New Product Item", function (transaction) {
    /* Add DREDD identificators to the new project data files */
    var newProduct = JSON.parse(transaction.request.body);
    var prefixText = 'DREDD TEST | ';
    newProduct.name = prefixText + newProduct.name;
    newProduct.subheading = prefixText + newProduct.subheading;
    newProduct.description = prefixText + newProduct.description;

    newProduct = JSON.stringify(newProduct);
    transaction.request.body = newProduct;
});

System configuration:

NodeJS: v5.7.0
NPM: v3.6.0
Dredd: v1.0.10
Express.JS: v4.13.1
API Blueprint

Most helpful comment

The only change required to make the test pass is the following diff to the swagger definition:

--- contract/min.json   2017-10-18 08:41:09.693730500 +0100
+++ contract/minplus.json       2017-10-18 08:46:48.036825400 +0100
@@ -27,7 +27,8 @@
                 ],
                 "responses": {
                     "200": {
-                        "description": "default"
+                        "description": "default",
+                        "schema": {}
                     }
                 }
             }

All 10 comments

Ok, I missed the model of the response in the Apiblueprint file

Corrected Apiblueprint file:

...
## Product Item [/api/products/product]
+ Model (application/json; charset=utf-8)
    + Attributes (LoadedProductItem)
    + Body

            { 
                ...
            }

    + Schema   

            { 
                ...
            }

...

### Save New Product Item [POST /api/products/product]
+ Attributes (NewlyCreatedProductItem)
+ Request (application/json)
+ Response 201

         [Product Item][]

...

@webmato Hi, I'm glad you managed to solve the issue :) Please note the + Model is a sort of old syntax to bring modularity to your API Blueprint. + Attributes and MSON alone should be already more convenient and powerful than + Model.

Just hit this same problem. Isn't the error message regarding EOF a bit misleading, when the true issue is a missing response model?

@MikeRalphson How exactly the erroneous blueprint look like? There are some sections left out from the original poster's blueprint. Would you be able to provide a minimal failing version of it? We could add it to the test suite and figure out how to properly report about the problem.

@honzajavorek In my case it was a Swagger/OpenAPI definition. I'll try and get a minimal testcase to you.

@MikeRalphson Please do, this is getting interesting. If it's Swagger, it could be a completely new issue. Also post the solution to the problem which works for you, please 馃檹

The only change required to make the test pass is the following diff to the swagger definition:

--- contract/min.json   2017-10-18 08:41:09.693730500 +0100
+++ contract/minplus.json       2017-10-18 08:46:48.036825400 +0100
@@ -27,7 +27,8 @@
                 ],
                 "responses": {
                     "200": {
-                        "description": "default"
+                        "description": "default",
+                        "schema": {}
                     }
                 }
             }

@MikeRalphson Thanks so much! This looks like a new bug. Either in https://github.com/apiaryio/dredd-transactions or https://github.com/apiaryio/fury-adapter-swagger. We should file it here on Dredd separately and inspect it further, probably with @kylef's help.

@honzajavorek excellent - just let me know if you want me to raise the new issue i.e. to avoid duplication.

@MikeRalphson Please do if you can - I'm author of way too many issues on this repo already 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webmaven picture webmaven  路  4Comments

honzajavorek picture honzajavorek  路  3Comments

axelssonHakan picture axelssonHakan  路  3Comments

philiplinell picture philiplinell  路  5Comments

romansklenar picture romansklenar  路  4Comments