I'm trying to implement a payment gateway from my back-end and I'm using 'payhere' checkout-api which is an external payment api. After sending a post request to the API it's supposed to redirect the user to the checkout page of payhere. But since I'm doing this through my own back-end api of my project I'm using the following method
res.set(data.headers);
res.write(data.body);
res.end();
the code worked fine when I called my own api through postman but when I tried it with a html file I created locally on my machine it does't display anything. The code of my html file is as follow
fetch('https://digicense-api.herokuapp.com/users/fines/pay', {
method: 'POST',
redirect: 'follow',
headers: {
'Content-Type': 'text/html',
'Authorization': 'Bearer _token_',
}
}).then(function (res) {
console.log(res)
return JSON.parse(res)
}).then(function (data) {
console.log(data.json())
})
How do I fix this so that the browser will display the page that is redirected by the external api? Thanks in advance!
Related
I have an input string that contains a partial URL such as "wikipedia.org" and I want to get the full URL "https://www.wikipedia.org/" using Node or JavaScript.
Is there a standard way to do this?
The problem is not knowing if the URL is HTTP or https and I would rather not make two API calls to test each case.
That problem can be solved by calling a specific API that provides SSL Verify checks.
As an example you can use rapidapi.
const axios = require("axios");
const options = {
method: 'POST',
url: 'https://ssl-certificate-checker.p.rapidapi.com/ssl-certificate',
headers: {
'content-type': 'application/json',
'X-RapidAPI-Key': '9a02e1bd8emsh83423276ecdc759p153572jsn874d28bed296',
'X-RapidAPI-Host': 'ssl-certificate-checker.p.rapidapi.com'
},
data: '{"port":"443","url":"wikipedia.org"}'
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
For details you can check the site of the API.
Click here to check articles and solutions to similar questions.
I'm working in Javascript (frontend) and have a colleague working in the backend with NodeJS.
When calling a GET request, he asks me to put the data in the body, but I could not figure out how to do that. (If I use this code to a POST request, it works fine).
Could you tell me if this is possible and how to do it? He says that it is possible, but I've googled a lot and could not find the correct way to do that.
ERROR that I get: "Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body."
let URL = "http://localhost:3000/verifyUser";
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NjJDMTRBNk";
fetch(URL, {
method: request,
mode: 'cors',
body: JSON.stringify({
user: 'Carlos6',
password: '543534543',
email: "algo6#gmail.com"
}),
headers: {
'Accept' : 'application/json',
'Content-type': 'application/json; charset=UTF-8',
'auth-token': token
}
}).then(function (response) {
if (response.ok) {
return response.json();
}
return Promise.reject(response);
}).then(function (data) {
console.log(data);
}).catch(function (error) {
console.warn('Something went wrong.', error);
});
You are using HTTP GET and sending a body.
If you want to send a body (JSON) you should use the PUT and POST.
The best will probably be to:
change your client code to method: "PUT"
change the server to access PUT request
If you want to know which one to chose look at this question:
( PUT vs. POST in REST)
If you wish to send a request with a body then you should make a POST-request and not a GET one. GET-request cannot have a body by its nature and primary goal.
All params of GET-request must be indicated in the URL itself only.
I'm sort of new to REST..
For full disclosure, I'm running this code inside of a Netlify Lambda function and testing via netlify-lambda.
My curl command works:
curl -u "<username>:<password>" https://api.github.com/repos/<username>/<reponame>
But when I attempt a get request via axios I'm getting a 404 (which according to github docs implies an auth issue). This is what I'm doing (also doesn't work without the custom headers, I've just been trying random things).
axios({
method: "get",
url: `https://api.github.com/repos/${user}/<reponame>/`,
headers: {
Authorization: `Bearer ${githubToken}`,
"Content-Type": "application/json"
},
auth: {
username: user,
password: pass
}
})
.then(res => {
callback(null, {
statusCode: 200,
body: JSON.stringify(res.data)
});
})
.catch(err => {
callback(err);
});
One thing I noticed was that it seems axios was taking my username and password and prepending them to the url i.g. https://<username>:<password>#api.github.com/repos/<username>/<reponame>
Is this how auth should be sent over?
I shouldn't have had a trailing forward slash at the end of my URL.
If you already have a token you don’t need user/pass, just add the token to the header.
I have the following code that makes a post request to my Nodejs app on the backend:
fetch('http://localhost:3000/ws', {
method: 'POST',
body: JSON.stringify(data),
headers: {'Content-Type': 'application/json'}
}).then(res => {
return res.json();
}).then(data => {
alert(JSON.stringify(data));
});
Everything works fine, it sends the request and gets the response successfully. The problem is that most of the browsers allows the users to debug my code, they can put a breakpoint where the response is being handled and modify its values.
How can I prevent them from doing that?
You can't apply that kind of control to code running on the client. You should always assume it's untrusted code running on the client. If you need controls, you'll have to apply them on the server.
I need to validate Bigcommerce legacy API credentials using AJAX (more precisely, using $http in AngularJS)
Every time I post a request to a store api, I always get the response:
[{"status":401,"message":"No credentials were supplied in the request."}]
I've tried every combination of parameters and headers that I can think of, and still I get the same error message. I could not find a single example of javascript code to validate the old Bigcommerce API credentials (legacy API).
Please help!
Bigcommerce clients provide the following data:
username, api_path, api_token
Here is my code:
var encoded_access_token = window.btoa(unescape(encodeURIComponent(
$scope.merchant.username + ':' + $scope.merchant.api_token
)));
$scope.merchant.api_path = 'https://store-ji3ql.mybigcommerce.com/api/v2/time';
$http({
method: 'GET',
url: $scope.merchant.api_path,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, application/xml, text/plain',
'Authorization': 'Basic ' + encoded_access_token
},
isArray: false
}).success(function(returnData) {
console.log('success!');
console.log(returnData);
}).error(function(returnData) {
console.log('error!');
console.log(returnData);
});
As I know, you could not validate Bigcommerce legacy API credentials with JS but a backend language such PHP, Ruby, ...
You could employ BC's APIs on you own server with PHP then expose it for JS code to call from.
(I have not implemented on my own, but that's what I heard from a bigcommerce developer)
Please visit this for more information: https://developer.bigcommerce.com/api/clients