question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
MikeRalphsoncommented, Oct 18, 2017

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": {}
                     }
                 }
             }
1reaction
honzajavorekcommented, Oct 19, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Get requires Content-Type application/json;charset=UTF-8
Here is my latest attempt,it still throws the error. Body.Headers.ContentType = new MediaTypeHeaderValue("application/json;charset=UTF-8");.
Read more >
Dredd - Release latest Apiary
If body parameter has schema.example (spec), it is used as a raw JSON sample for the request body. If it's not present,.
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
This string has to be valid JSON and will throw this error if incorrect syntax was encountered. Examples. JSON.parse() does not allow trailing...
Read more >
Testing an API Against Documentation with Dredd
Expected body Content-Type is application/json ; charset=utf-8 but body is not a parseable JSON: Parse error on line 1: + Attributes (Task) ...
Read more >
Can some one help me how to verify the pact.json file from the ...
new String[] { "Content-Type", "application/json;charset=UTF-8" }); ... willRespondWith().headers(headers).status(200).body(new PactDslJsonBody().
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found