【问题标题】:working with API coded in hapi使用以 hapi 编码的 API
【发布时间】:2017-05-30 13:51:52
【问题描述】:

我正在使用未记录的 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}]

我做错了什么?

【问题讨论】:

    标签: javascript rest api post hapijs


    【解决方案1】:

    看起来端点需要一个与有效负载对象中的键 responses 关联的数组(提示:if (request.payload.responses))。

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

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

    【讨论】:

    • 看起来大概就是这样。今晚试试。
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 2021-10-24
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多