Nathan
Nathan

Reputation: 107

working with API coded in hapi

I'm working with a RESTful api that is undocumented. However the code behind it is opensource. It seems to be written in a combination of hapi and Bookshelfjs. I'm trying to figure out how to send a POST request to one of the routes, but it's not working. Below is the code to the route:

{
    method: 'POST',
    path: '/api/survey_answer',
    handler: (request, reply) => {
      if (request.payload.responses) {
        Promise.all(
          request.payload.responses.map(function(answer) {
              var surveyAnswer = new SurveyAnswer()
              var newAnswer = surveyAnswer
              .save({
                survey_response_id: answer.surveyResponseId,
                question_id: answer.questionId,
                answer_id: answer.answerId,
                intensity: answer.intensity
              })
              .catch(function(err) {
                console.error(err)
              })

              return newAnswer
          })
        ).then(function (newSurveyAnswers) {
          reply(newSurveyAnswers)
        })
      }
    }
},

This is my POST request:

POST /api/survey_answer HTTP/1.1
Host: 192.168.145.129:3000
Content-Type: application/json
Cache-Control: no-cache

[{"surveyResponseId":1,"questionId":1,"answerId":1,"intensity":3}]

What am I doing wrong?

Upvotes: 1

Views: 51

Answers (1)

Ankh
Ankh

Reputation: 5718

It looks like the endpoint is expecting an array that's associated to a key responses in the payload object (hint: if (request.payload.responses)).

Try changing your POST payload to be:

{"responses": [{"surveyResponseId": 1,"questionId": 1,"answerId": 1,"intensity": 3}]}

Upvotes: 1

Related Questions