Reputation: 23
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.""
Upvotes: 2
Views: 4329
Reputation: 201428
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.
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)
X-API-TOKEN
, URL and other parameters which need to your situation.Upvotes: 1