I am using zafClient to perform requests as per this article:
https://developer.zendesk.com/apps/docs/core-api/client_api#client.requestoptions
So far I am successful with GET requests and can fetch the information needed. Example:
zafClient.request({
url: '/api/v2/macros.json',
httpCompleteResponse: true,
}).then(response => {
console.log(response.responseJSON);
}, error => {
console.log(error.responseText)
})
but, when I perform POST operation, I am getting nasty error that I cannot fight.
here is post:
zafClient.request({
url: '/api/v2/macros.json',
method: 'POST',
data: {
"macro": {
'title': "Created Macro with API test",
'actions': [{ "field": "subject", "value": "Change subjectline with API" }]
}
}
}).then(res => console.log(res), error => console.log(error.responseText))
And here is the error.responseTest
{
"error": {
"title": "Invalid attribute",
"message": "You passed an invalid value for the actions attribute. Invalid parameter: actions must be an array from api/v2/rules/macros/create"
}
}
Am I missing something in the post operation? if I log just the object "macro" I can see that attribute "actions" is an array, so the object "macro" is an exact object that I am fetching from API
Any ideas would be welcome as to what am I doing wrong.
Now I know that I was supposed to JSON.Stringify passing data :)
zafClient.request({
url: '/api/v2/macros.json',
httpCompleteResponse: true,
contentType: 'application/json',
method: 'POST',
data: JSON.stringify({
macro: {
title: "Created Macro with API test",
actions: [{ "field": "subject", "value": "Change subjectline with API" }]
}
})
}).then(res => console.log(res), error => console.log(error.responseText))
First experience with HTTP requests
Related
I am trying to integrate https://www.checkout.com/ in my app.
I tried there sample code to get token.
Frames.addEventHandler(
Frames.Events.CARD_TOKENIZED,
function (data) {
Frames.addCardToken(form, data.token);
if (data.token) {
confirmPayment(data.token);
} else {
console.log(data);
}
}
);
I tried https://api.sandbox.checkout.com/payments/ api to do the payments,
payment successfully captures, but I am unable to read its response to or redirect user on successfully attempt.
async function confirmPayment(token) {
// Storing response
const response = await fetch('https://api.sandbox.checkout.com/payments/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk_sbox_..abc'
},
body: JSON.stringify({
"source": {
"type": "token",
"token": token
},
"Capture": true,
"processing_channel_id": "pc_..abc",
'amount': 1000,
'currency': 'USD',
"3ds": {
"enabled": false
},
"customer": {
"email": "test.testUI#example.com",
"name": "John Test"
},
'reference': 'ORD-175-759',
"metadata": {
"udf1": "UI-CALL-TEST",
"coupon_code": "NY2018",
"partner_id": 123989
},
"success_url": "http://example.com/payments/success",
"failure_url": "http://example.com/payments/fail"
})
});
// Storing data in form of JSON
var data = await response.json();
console.log(data);
if (response) {
}
show(data);
}
I want to get a payment response like errors "card invalid" etc
Also redirect to Url if successful.
Please let me know if this is a correct way to do the payment or If there any JavaScript library that I can use?
I want to do payment on client side and use webhook later.
Thank you
I'm doing an AJAX GET request to an API that returns all stores in my area that offer express delivery. I need to check over the data to see if any store in the area offer express delivery but can't access the data properly to perform any check's and don't understand why.
A stripped back version of the data returned from the API is here:
{
"data": {
"service_eligibility": [
{
"accesspoint_list": [
{
"current_time": null,
"currency": null,
"accesspoint_id": "3242342-6dc1-43d8-b3d0-234234234234",
"service_info": {
"fulfillment_type": "DELIVERY",
"reservation_type": "SLOTS",
"dispense_type": "DELIVERY",
"priority": 0,
"carrier": "Owned",
"fulfillment_option": "DELIVERY",
"location_type": "Home",
"service_time": 0,
"is_recurring_slot": false,
"enable_express": false,
"is_unattended_slot": true,
"is_flex_slot": false
},
{
"current_time": null,
"currency": null,
"accesspoint_id": "234234242-3387-4e1d-9eaa-234234242",
"service_info": {
"fulfillment_type": "INSTORE_PICKUP",
"reservation_type": "SLOTS",
"dispense_type": "PICKUP_INSTORE",
"priority": 0,
"carrier": "NA",
"fulfillment_option": "PICKUP",
"location_type": "Store",
"service_time": 0,
"is_recurring_slot": false,
"enable_express": true,
"is_unattended_slot": false,
"is_flex_slot": false
},
"current_time": "2021-06-07T11:24:39Z",
"currency": "GBP"
},
"status_code": 200,
"status_message": "Requested input executed successfully."
}
The call I use to get my data:
$.ajax({
type: 'GET',
dataType: "json",
url: query,
data: "",
success: function(data)
{
//Check for express delivery
if(data.status_code == 200) {
console.log(data);
}
},
error: function(data)
{
//Check for API error
if(data.status == 400){
console.log(data.responseJSON.errors[0].message);
}
}
})
So far the data is being logged to the console. But when I try to access it or parts I want to use I get console errors.
I have tried copying the property path from the console and using it in the console.log:
console.log(data.service_eligibility[0].accesspoint_list)
but get the error message:
Cannot read property '0' of undefined
I have tried just data.service_eligibility but this just give me the error of undefined
In fact even if I just try to access data.currency I get undefined
Why can I not access the data being returned from the API?
type: 'GET',
dataType: "json",
url: query,
data: "",
success: function(data)
{
//Check for express delivery
if(data.status_code == 200) {
console.log(response.data.ervice_eligibility[0].accesspoint_list);
}
}
Looking at the above piece of code you have provided, it looks like, the data object that you are receiving in the success handler does not directly refer to the data object inside your received response. Given the names are same, it can be confusing.
I think if you try accessing the property inside the data object using data.data.service_eligibility, you will be able to access it. For some more clarity, see the code below:
type: 'GET',
dataType: "json",
url: query,
data: "",
success: function(response)
{
//Check for express delivery
if(response.status_code == 200) {
console.log(response.data.service_eligibility[0].accesspoint_list);
}
}
Just think about it, if your data is available inside the success handler, it means that the asynchronous action of fetching the data from the api has successfully completed. You can just debug the structure of your data object and find out what's missing or what's wrong in the way you are accessing a value. People can behave differently, code can never! :)
I have a project with Cloud Firestore as database. Now I want to update the data in one document using the fetch method. My Cloud Firestore stucture is the following:
Logging (Collection)
[userID] (Document)
Notifications (Collection)
[notificationID] (Document)
active: "true"
type: "T1"
And I use the fetch call below:
fetch("https://firestore.googleapis.com/v1/projects/[ID]/databases/(default)/documents/Logging/[userID]
+"/Notifications/[notificationID]?updateMask.fieldPaths=active", {
method: 'PATCH',
body: JSON.stringify({
"active": "false"
}),
headers: {
Authorization: 'Bearer ' + idToken,
'Content-Type': 'application/json'
}
}).then( function(response){
console.log(response);
response.json().then(function(data){
console.log(data);
});
}).catch(error => {
console.log(error);
});
Executing the fetch method I'm running in an error with message
"Invalid JSON payload received. Unknown name "active" at 'document': Cannot find field."
How can I update the existing fields of a document of Firestore with the REST API? Can anyone help me out? I've tried a lot of different "urls" and methods, but nothing works for me.
As explained in the Firestore REST API doc, You need to pass an object of type Document in the body, as follows:
{
method: 'PATCH',
body: JSON.stringify({
fields: {
active: {
stringValue: 'false',
},
},
}),
}
I made the assumption that your active field is of type String (since you do "active": "false"). If it is of type Boolean, you need to use a booleanValue property instead, as follows. See this doc for more details.
{
method: 'PATCH',
body: JSON.stringify({
fields: {
active: {
booleanValue: false,
},
},
}),
}
I am trying to create a fetch call to servicedesk plus system but I keep getting 400 error, I understand that the request I made is not what the server wanted but in postman, I do manage to send that same query successfully. I can't understand what is the problem
In the dev tools network tab I see this data after dending the Json:
response_status: {status_code: 4000,…}
messages: [{status_code: 4001, field: "input_data", type: "failed", message: "JSON cant be analyzed"}]
0: {status_code: 4001, field: "input_data", type: "failed", message: "JSON cant be analyzed"}
field: "input_data"
message: "JSON cant be analyzed"
status_code: 4001
type: "failed"
status: "failed"
status_code: 4000
From what I read in the API documentation I saw that the 4001 error means that the ID or the name are not as they are in the service desk system.
here is a link to the API docs
this is my code:
const jss = {
"request": {
"subject": "test 29072020",
"description": "TEST REQUEST BY DAVID",
"requester": {
"id": "1231",
"name": "david"
},
"resolution": {
"content": "Mail Fetching Server problem has been fixed"
},
"status": {
"name": "פתוחה"
},
"template": {
"name": "Test for API",
"id": "123123"
},
}
}
const sendToSd = document.getElementById('send');
sendToSd.addEventListener('click', submitData);
var mydata = JSON.parse(jss);
var stringify = JSON.stringify(mydata);
async function submitData(){
const url = 'http://MYURL.COM/api/v3/requests?TECHNICIAN_KEY=SOME-STRING&input_data='+stringify+'&FORMAT=json';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
console.log('response',response)
}
This fetch works and manages to retrieve the data from the service desk using the same URL and TECHNICIAN_KEY.
// fetch('http://MYURL.COM/api/v3/requests/61627?TECHNICIAN_KEY=SOME-STRING', {
// method: 'GET'
// })
// .then(response => {
// console.log(response);
// return response;
// });
I am trying to figure out how the payload should be constructed for a Open Graph object to submit to yammer. I can post a standard message, but i would like to post an opengraph message instead.
{
"activity":{
"actor":{"name":"Sidd Singh",
"email":"sidd#xyz.com"},
"action":"create",
"object": {
"url":"https://www.sched.do",
"title":"Lunch Meeting"
},
"message":"Hey, let’s get sushi!",
"users":[
{"name":"Adarsh Pandit",
"email":"adarsh#xyz.com"}
]
}
}
This is some code nicked from their API documentation but doesn't show me how i should use this in javascript. Can someone assist me? Below is my existing code that posts a standard message...
yam.request({
url: "https://www.yammer.com/api/v1/messages.json?network_id=networkname", //this is one of many REST endpoints that are available
method: "POST",
beforeSend: function (req) { //send the access_token in the HTTP header
req.headers.Authorization = "Bearer " + access_token;
},
data: {
"network": "networkname",
"body": "Test Post",
"group_id": "3719771"
},
success: function (data) { //print message response information to the console
toastr.success('An Item was successfully posted to Yammer', "Yammer Network");
},
error: function (user) {
toastr.error('There was an error eith the request', "Yammer Network");
}
});
This post answered your question: Yammer Open Graph API Error 400
Simply replace the key value pairs in the data{} with the activity json strings. Also remember to change the RESTful api endpoint to https://api.yammer.com/api/v1/activity.json