API request javascript

I am trying to make an API request through Javascript via Google Sheets.
var data = {
'requestJobDescription':
{"type": "create",
"credentials":{
"partnerUserID":"ID",
"partnerUserSecret":"SECRET"
},
"inputSettings":{
"type": "policy",
"policyName": "My New Policy"
}
}
};
payload = JSON.stringify(data);
var options = {'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : payload};
When I try to execute this I get the following error:
{"responseMessage":"'requestJobDescription' is missing","responseCode":500}
Please could you assist?
Many thanks
Answers
-
Hey @mv91 !
Can you please share how you made the request? I'm not seeing anything in that code block that shows how you sent `payload` or `options` to our servers. I think knowing that will help me guide you better! Remember to redact any partner secrets/IDs if you are coping and pasting directly from your API call.
I look forward to your reply!
-
@mv91 ,
I am having the same issue. I first tried with a console application, then with SoapUI. I tried with a policy get, then with a report export. I receive the same exact response. Did you find a solution?
null
-
Hi there, I did manage to solve it.
The JSON stringify method was causing the error. If i change the 'payload' argument from my variable 'payload' to the variable 'data', then the above works.
Hopefully that makes sense and works for you - give me a shout if not and i'll try to explain/help a bit better.
-
@mv91,
Thanks for the response. I tried using your JSON in SoapUI and I'm still seeing the same error (and used my creds). Did you need to modify your JSON at all?
-
Additionally, I tried posting through another rest client, Insomnia, same error.
-
Oh, right. I'm unfamiliar with SoapUI but could you post your code here and I can try and have a play with it?
-
The following code snippet works:
var data = {
"type":"create",
"credentials":{
"partnerUserID":credentials.partnerUserID,
"partnerUserSecret":credentials.partnerUserSecret
},
"inputSettings":{
"type":"expenses",
"employeeEmail":credentials.employeeEmail,
"transactionList": transactions
}
}
var jobDescription = { "requestJobDescription": JSON.stringify(data) }
var url = "https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations";
var options = {
"method": "post",
"payload": jobDescription
};
response = UrlFetchApp.fetch(url, options);
-
const params = {
method: "post",
contentType: "application/x-www-form-urlencoded",
payload: `requestJobDescription=${JSON.stringify(requestJobDescription)}`
}
The default content type is "application/x-www-form-urlencoded", it can be removed form the params object.
-
Hi.
Do you mind put online line the code corrected? I can figure out what you have made to make it work.
I have been trying to make a Googlescript interact with Expensify but I alway get the same Error.
Here is My code
function createExpensifyReport() {
var reportName = "Mix_test";
var data = {
"type": "create",
"credentials": {
"partnerUserID": "My_partnerUserID",
"partnerUserSecret": "My_partnerUserSecret"
},
"inputSettings": {
"type": "report",
"report": {
"reportName": reportName
}
}
};
var options = {
"method": "post",
"headers": {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0"
},
"muteHttpExceptions": true,
"payload": JSON.stringify({
"requestJobDescription": "Create report",
"requestData": JSON.stringify(data)
})
};
Logger.log(options.payload)
var response = UrlFetchApp.fetch("[https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations"](https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations), options);
Logger.log(response.getContentText());
}
"
Evertime I run this googlescript code I get an error :
"
{"responseMessage":"'requestJobDescription' is missing","responseCode":500}
"
I don't see where I go it wrong.
the Logger.log gives me the right Payload :
"
{"requestJobDescription":"Create report","requestData":"{\"type\":\"create\",\"credentials\":{\"partnerUserID\":\"partnerUserID\",\"partnerUserSecret\":\"partnerUserSecret\"},\"inputSettings\":{\"type\":\"report\",\"report\":{\"reportName\":\"Ann_Mix_test\"}}}"}
"
Can anyone tell me where is my mistake?