使用hapi编码的API

时间:2017-01-15 17:48:03

标签: javascript rest api post hapijs

我正在使用未记录的RESTful api。然而,它背后的代码是开源。它似乎是用hapi和Bookshelfjs的组合编写的。我正在试图弄清楚如何向其中一条路线发送POST请求,但它无法正常工作。以下是路线的代码:

{
    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)
        })
      }
    }
},

这是我的POST请求:

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}]

我做错了什么?

1 个答案:

答案 0 :(得分:1)

看起来端点期望一个数组与有效负载对象中的密钥 "The spraysample Route" - { "simple" in { Get("/") ~> mapHttpResponse({x=>{println("response: "+x.toString);x}})(complete("logged")) ~> check { val r=responseAs[String] r === "logged" } } 相关联(提示:vazzeg: HttpResponse(200 OK,HttpEntity(text/plain; charset=UTF-8,logged),List(),HTTP/1.1) )。

尝试将POST有效负载更改为:

responses