Hey guys, In this post, I want to share with you a convenient way to POST and GET HTTP requests with IP authentication when you are working with 3rd party services.
As you know, WIX is a cloud service, and WIX IPs change all the time. This is why you should not rely on Wix's IP addresses for authentication.
But, in some cases, the 3rd party services require you to use Static IP for authentication.
I used webshare.io as a Proxy provider, axios NPM as an HTTP requests helper, and btoa NPM as Base64 Encoding helper.
After we open an account at webshare and configured the IP address (Takes 5 Min), we received an IP address, Port, user name, and password.
We will use them for every request we will make.
LMKWYT, Thanks
The code:
import wixSecretsBackend from 'wix-secrets-backend';
const axios = require('axios');
const btoa = require('btoa');
const url = 'https://0ab0201f4075e051627947b1f1835f05.m.pipedream.net'; // Replace with your endpoint url
export async function post(body) {
try {
const res = await axios.post(url, body, {
proxy: {
host: '193.8.56.119', // replace with your proxy IP
port: 9183 // replace with your proxy port
},
/*
Without end point url Authorization:
headers: {Proxy-Authorization : await getKey()}
*/
// With end point url Authorization:
headers: {
'Proxy-Authorization': await getKey(),
"Content-Type": "application/json",
"Authorization": 'AuthorizationKey' // replace with the end point Authorization key
}
})
if (res.status === 200) {
return res.data
}
throw new Error('Post failed');
} catch (err) {
throw new Error('failed to post - original error - ' + err.message);
}
}
export async function get() {
try {
const res = await axios.get(url, {
proxy: {
host: '193.8.56.119', // replace with your proxy IP
port: 9183 // replace with your proxy port
},
headers: { 'Proxy-Authorization': await getKey(), }
})
if (res.status === 200) {
return res.data
}
throw new Error('Get failed');
} catch (err) {
throw new Error('failed to get - original error - ' + err.message);
}
}
async function getKey() {
try {
const userName = await wixSecretsBackend.getSecret('proxyUserName');
const pass = await wixSecretsBackend.getSecret('proxyPass');
const proxyApiKey = 'Basic ' + btoa(userName + ":" + pass);
return proxyApiKey
} catch (err) {
throw new Error('failed to getKey - original error - ' + err.message);
}
}
Thanks, my issue has been fixed.
I did the first step got the proxy how do I use axios NPM as an HTTP requests helper, and btoa NPM as Base64 Encoding helper. How and where do I use your code, or if its to much to ask maybe I can youtube video it all?
i use this code but static IP through this error "failed to get - original error - write EPROTO 140462069368640:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:"
Can this strategy be used with any proxy provider?