获取'httpStatus 400 - Bad Request'时如何修复POST API调用?

问题描述 投票:1回答:1

我的目标是从使用Google Apps脚本在Qualtrics上进行的调查中导出回复。我试图让我的代码工作一个POST API,我得到了ping的代码,但不管它是什么ping,它回来时出现'httpStatus:400-Bad request'错误。

我是Google Apps脚本和使用API​​的新手,但了解它的要点。我使用Postman获取了一个javaScript Jquery ajax代码并使其与GAS一起使用。让我感到困惑的是,当我使用与GET API相同的代码并手动输入ID(使用POSTMAN给我)时,它完美地砰然一声。当它通过Postman运行时,它表明一切都在进行,所以不确定我在POST调用中做错了什么。

var option = {
  async: true,
  crossDomain: true,
  //url:"https://ousurvey.ca1.qualtrics.com/API/v3/surveys/SV_8dK8AKUFyAH8qyN//export-responses/",
  method: "POST",
  "headers": {
    "X-API-TOKEN": "**************************",
    "Content-Type": "application/json",
    "cache-control": "no-cache",
    "Postman-Token": "7a148b75-fa03-4f45-9782-08791c2f1c35"
  },
  processData: false,
  data : '{"format": "csv}',
  muteHttpExceptions: true //muted to check Logger
   };
 var qUrl='https://ousurvey.ca1.qualtrics.com/API/v3/surveys/SV_8dK8AKUFyAH8qyN/export-responses/'
 var getSurvey = UrlFetchApp.fetch(qUrl, option);

我需要让POST工作以获取JSON以获取调查ID,以便我可以将该ID与GET API一起下载到google驱动器并将信息转换为GoogleDocs。

以下是日志中的当前错误:

{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Error decoding json body:
 com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input\n at 
[Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1@71f9c2bb; line: 1, column: 0]"}}}

将“Content-Type”更改为“contentType”后,我收到此错误:

""meta":{"requestId":"62b3a313-b1ba-4939-83b7-ee73e65b4e3e","httpStatus":"400
 - Bad Request","error":{"errorCode":"QVAL_1","errorMessage":"Json type request body is expected.""
javascript api post google-apps-script qualtrics
1个回答
0
投票

从您的问题和回复评论中,我可以像上面那样理解。当我看到您提供的文档时,我发现了示例curl命令,如下所示。

curl -X POST \
-H 'X-API-TOKEN: yourapitokenhere' \
-H 'Content-Type: application/json' \
-d '{"format": "csv"}' \
'https://yourdatacenterid.qualtrics.com/API/v3/surveys/SV_012345678912345/export-responses'

我将此示例转换为Google Apps脚本的脚本。示例脚本如下。

Sample script:

var qUrl = "https://ousurvey.ca1.qualtrics.com/API/v3/surveys/SV_8dK8AKUFyAH8qyN/export-responses/";
var option = {
  method: "post",
  headers: {"X-API-TOKEN": "###"},
  contentType: "application/json",
  payload: JSON.stringify({"format": "csv"}),
  muteHttpExceptions: true,
};
var getSurvey = UrlFetchApp.fetch(qUrl, option);
Logger.log(getSurvey)

Note:

  • 上面的示例脚本与sample curl命令的请求相同。但是如果在运行脚本时发生错误,请确认X-API-TOKEN,URL和其他需要您的情况参数的值。

References:

© www.soinside.com 2019 - 2024. All rights reserved.