如何在获取“ httpStatus 400-错误请求”时解决POST API调用?

时间:2019-04-24 17:05:26

标签: javascript api post google-apps-script qualtrics

我的目标是从使用Google Apps脚本对Qualtrics进行的调查中导出回答。我正在尝试使我的代码能够使用POST API,并且可以对ping进行编码,但是无论ping为何,它都会返回“ httpStatus:400错误请求”错误。

我对Google Apps脚本和API都是陌生的,但了解它的要旨。我使用Postman获取了JavaScript Jquery ajax代码,并将其与GAS一起使用。令我感到困惑的是,当我将相同的代码与GET API一起使用并手动输入ID(使用POSTMAN给我)时,它会ping通。通过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.""

1 个答案:

答案 0 :(得分:0)

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

import numpy as np
import pandas as pd
import re

>>>df
name    number      date
a       1.2         123.2
b       123.2       3/13/2019
c       2.3         3/14/2019
d       569         3/15/2019
e       abc         3/15/2019
f       30          abc 
g       39.8        -3
h       3/21/2019   3/19/2019
i       -395        3/20/2019
j       4           3/21/2019


# Matches m/dd/yyyy
dt_pattern = r'[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}'
dt = re.compile(dt_pattern)
# alphabet strings
wd_pattern = r'[a-zA-Z]+'
wd = re.compile(wd_pattern)

df.loc[:,'date'] = [d if dt.match(str(d)) else np.nan for d in  df.loc[:, 'date']]
df.loc[:,'number'] = [d if not any([wd.match(str(d)), dt.match(str(d))]) else np.nan for d in  df.loc[:, 'number']]
df['date'] = pd.to_datetime(df['date'], errors='coerce')

>>>df
  name number       date
0    a    1.2        NaT
1    b  123.2 2019-03-13
2    c    2.3 2019-03-14
3    d    569 2019-03-15
4    e    NaN 2019-03-15
5    f     30        NaT
6    g   39.8        NaT
7    h    NaN 2019-03-19
8    i   -395 2019-03-20
9    j      4 2019-03-21

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

示例脚本:

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'

注意:

  • 以上示例脚本与sample curl命令是相同的请求。但是,如果在运行脚本时发生错误,请确认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) 的值,URL和其他您需要的参数。

参考文献: