Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Hopeful fix to non stringified bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Donald Robertson committed Apr 27, 2017
1 parent 3072c4f commit 2a208ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/contract/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ Contract.prototype.validate = function (cb) {
if (this.after) { afterHooks = this.after.map(mapHook) }

const execute = (done) => {
const shouldBeJson = (body) => Array.isArray(body) || typeof body === 'object'
const sanitiseBody = (body) => shouldBeJson(body) ? JSON.stringify(body) : body

if (this._request.body) {
this._request.body = sanitiseBody(this._request.body)
}

this._client(this._request, (err, res, body) => {
if (err) { return done(new Error(`Request failed: ${err.message}`)) }

Expand Down
9 changes: 4 additions & 5 deletions lib/contract/map-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
const request = require('request')
const buildUrl = require('../build-url')

const hookBodyIsJson = (body) => Array.isArray(body) || typeof body === 'object'
const shouldBeJson = (body) => Array.isArray(body) || typeof body === 'object'
const sanitiseBody = (body) => shouldBeJson(body) ? JSON.stringify(body) : body

module.exports = (hook) => (done) => {
const url = hook.baseUrl
Expand All @@ -17,12 +18,10 @@ module.exports = (hook) => (done) => {
url: url,
timeout: hook.timeout,
headers: hook.headers,
body: hook.body
body: sanitiseBody(hook.body)
}

if (hookBodyIsJson(hook.body)) {
requestParameter.json = true
}
console.error(typeof requestParameter.body)

request(requestParameter, (err) => {
if (err) { return done(err) }
Expand Down

0 comments on commit 2a208ae

Please sign in to comment.