2

My goal is to export responses from a survey I made on Qualtrics using Google Apps Script. I am trying to get my code to work the a POST API and I got the code to ping, but whatever it is pinging, it is coming back with an 'httpStatus: 400-Bad request' error.

I am new to both Google Apps Script and using API but understand the gist of it. I used Postman to acquired a javaScript Jquery ajax code and made it work with GAS. What is confusing me is when I use the same code with GET APIs and manually typing in IDs (given to me using POSTMAN), it pings perfectly. When running it through Postman, it shows that everything is going through, so not sure what I am doing wrong with the POST call.

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);

I need to get the POST to work to obtain the JSON to get the surveys ID so I can use that ID with the GET API to download the information to google drive and convert the information into GoogleDocs.

Here is the current error from the log:

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

After changing "Content-Type" to "contentType" I get this error:

""meta":{"requestId":"62b3a313-b1ba-4939-83b7-ee73e65b4e3e","httpStatus":"400
 - Bad Request","error":{"errorCode":"QVAL_1","errorMessage":"Json type request body is expected.""
nguyenJEN
  • 23
  • 1
  • 4
  • Your data should probably just be an object, i.e., `{ "format" : "csv" }`, not a string. – Chris White Apr 24 '19 at 17:12
  • No luck, I tried doing that and still pings a bad request – nguyenJEN Apr 24 '19 at 17:34
  • Do you have the error message / code ? Something like "Expected _ but got _" ? – Katherine R Apr 24 '19 at 20:33
  • I updated the post with the error message I get, along with the change for format on the data. – nguyenJEN Apr 24 '19 at 21:09
  • Can you provide the document of API? And I thought that this information might be useful for you. https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params But if this was not useful, I apologize. – Tanaike Apr 25 '19 at 00:48
  • Here is the webpage with the API info: https://api.qualtrics.com/reference#close-session. I am using the POST/GET/GET to do 'Survey Response Exports" I made a little bit of progress, I changed "Content-Type" to "contentType" and the error it gives shows a little it of progress. – nguyenJEN Apr 25 '19 at 01:14
  • @nguyenJEN Thank you for replying. I proposed a sample script as an answer. Could you please confirm it? Recently, I have experienced that the error occurred even when the request which is the same with the curl command is used. [Ref](https://stackoverflow.com/questions/55758941/salesforce-bulk-job-csv-upload-via-google-apps-script-urlfetchapp) So I'm not sure whether above script is the direct solution of your issue. I apologize for this situation. – Tanaike Apr 25 '19 at 02:22

1 Answers1

1

From your question and replying comment, I could understand like above. When I saw the document you provided, I found the sample curl command as follows.

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'

I converted this sample to the script of Google Apps Script. The sample script is as follows.

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:

  • Above sample script is the same request with the sample curl command. But if an error occurs when you ran the script, please confirm the value of X-API-TOKEN, URL and other parameters which need to your situation.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165