I am posting data to a web API. The response is a 401 error. I don't know what is wrong with my code.
This is the code:
var form = document.getElementById("form");
form.addEventListener("submit", function (e) {
e.preventDefault();
var edition= document.getElementById("edition").value;
var competition= document.getElementById("competition").value;
var price= document.getElementById("price").value;
var firstname= document.getElementById("firstname").value;
var lastname= document.getElementById("lastname").value;
var email= document.getElementById("email").value;
fetch("https://front-api.njuko.com/partner/registration", {
method: "POST",
body: JSON.stringify({
edition: edition,
competition: competition,
price: price,
firstname: firstname,
lastname: lastname,
email: email,
}),
headers: {
"Content-Type": "application/json",
},
})
.then(function (response) {
return response.json();
})
.then(function (data) {
console.log(data);
});
});
As i am new to the programming i don't how i can send the data into the website. This are the request sample of the website.
{
"edition": "5d888c93df85b80006f54265",
"competition": "5d888c93df85b80006f54265",
"price": "5d888c93df85b80006f54265",
"firstname": "John",
"lastname": "Doe",
"email": "john.doe#acme.com",
"methodName": "Partner payment (VISA)",
"datas": [
{
"value": "Detroit, Michigan",
"field": "5d888c93df85b80006f54255"
}
],
"metaData": [
{
"key": "stravaActivityId",
"label": "strava-activity-id",
"value": "329859FNENF29492Ifnjnfe93"
}
]
}
This are the Actual Picture
401 Error Code means Unauthorized.
I don't see any authentication in your code.
The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.
I dont know the implementation of the exact API,
but mostly used is this :
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer <API_TOKEN>"
}
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 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’m using Node js, and I can create/update a contact’s email, name, or phone number. But the custom fields I have never get updated. Here’s what I got so far.
var data = JSON.stringify({
"contact": {
"email": "t#brady.com",
"firstName": "Tom",
"lastName": "Brady",
"phone": "111122233",
"myCustomField": "myValue"
}
});
var options = {
hostname: hostname,
path: '/api/3/contact/sync',
method: 'POST',
headers: {
'Api-Token': apiToken,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
}
var req = this.https.request(options, function(res){
});
req.on('error', function(err){
console.log('error: ' + err.message);
});
req.write(data);
req.end();
So this will update the contact's built-in fields (email, name, phone) but not myCustomField. Any idea why? How to solve it? I would really appreciate any help.
P.S. myCustomField exists in Active Campaign. The contact just doesn't have a value for it.
First, you need to get the filed id of the custom fields. You can do this with postman with GET request https://youraccountname.api-us1.com/api/3/fields docs and when you have the ids that you want to update you can easily do it like this. Hope it help. I was struggling a lot before I figured this out.
fetch(proxyurl + active_campaign_url, {
mode: "cors",
method: "POST",
body: JSON.stringify({
contact: {
firstName,
lastName,
email,
fieldValues: [
{
field: 1,
value: jobTitle,
},
{
field: 30,
value: companyName
}
],
},
}),
headers: headers,
})
.then((response) => response.json())**strong text**
I am making an application that sends emails from a User as described by this article.
Everything is working as expected, except for when I try to include an attachment. The email sends, but without the attachment. I'm not sure what the problem is as I've tried pretty much everything I could find online. I have made sure the file I am sending is properly encoded in base64.
var message = {
"subject": subject,
"hasAttachments":true,
"body": {
"contentType": "Text",
"content": emailBody
},
"toRecipients": toRecipients,
ccRecipients,
bccRecipients
};
function sendMailRequest(access_token, message, uriSend, file, base64, callback){
const attachments = [{
'#odata.type': '#microsoft.graph.fileAttachment',
"contentBytes": base64
"name": "example.jpg"
}];
// Configure the request
var options2 = {
"url": uriSend,
"method": 'POST',
"headers": {
'Authorization': access_token,
'Content-Type': 'application/json'
},
"body": JSON.stringify({
"message": message,
"SaveToSentItems": "true",
"Attachments": attachments
})
}
Attachments go inside the message JSON, not outside of it. This should work:
function sendMailRequest(access_token, message, uriSend, file, base64, callback) {
const attachments = [
{
"#odata.type": "#microsoft.graph.fileAttachment",
"contentBytes": base64
"name": "example.jpg"
}
];
message["attachments"] = attachments;
// Configure the request
var options2 = {
"url": uriSend,
"method": "POST",
"headers": {
"Authorization": access_token,
"Content-Type": "application/json"
},
"body": JSON.stringify({
"message": message,
"SaveToSentItems": "true"
})
}
...
}
I'm building a mobile client to talk to a bot built with microsoft botbuilder through botconnector directline. I want to send things like the unique id of the user speaking with the bot, so my bot can operate on this user. Currently I'm just POSTing to directline, but when I add additional things in the body, my bot can't retrieve it. I'm probably doing something really simple wrong. Would love to get your help on this!
POST code from client:
sendToBot: function(setUpObj, message, returnCallback){
var postURL=baseURL+"/"+setUpObj.conversationId+"/messages"
var postOptions ={
method: 'POST',
headers: {
"Authorization": setUpObj.conversationToken,
"content-type": "application/json"
},
body: JSON.stringify({
"text": message,
"from": {
'address': setUpObj.currentUserUid
}
})
}
fetch(postURL, postOptions)
.then(
(response)=>response.text()
)
.then(
(responseText)=>{
this.getResponse(setUpObj, returnCallback)
}
)
}
and I'm accessing the the currentUserUid on the server by
session.message.from.address
Thanks for you patience.
Have you tried setting the channelData in the json? It's described as "data sent unmodified between client and bot" and can look like:
{
"id": "CuvLPID4kDb|000000000000000004",
"conversationId": "CuvLPID4kDb",
"created": "2017-02-22T21:19:51.0357965Z",
"from": "examplebot",
"text": "Hello!",
"channelData": {
"examplefield": "abc123"
}
}
https://docs.botframework.com/en-us/core-concepts/channeldata/
So in your code it could look like:
var postOptions ={
method: 'POST',
headers: {
"Authorization": setUpObj.conversationToken,
"content-type": "application/json"
},
body: JSON.stringify({
"text": message,
"channelData": {
"from": {
"address": setUpObj.currentUserUid
}
}
})
}