@QMcKay maybe you can help me. I am writing an integration with a third party API that requires Basic Auth before any requests can be made. They do not allow browser access like https://username:password@api.blah.com/v1/access
I am thinking we do not have a helper for this… I can authenticate with POSTMAN and tested Curl it works by using:
CURL https://api.blah.com/v1/access \
-u apikey:
Am I right to say we do not have a method for authentication or is there some hidden JSCRIPT i can use to authenticate?
Both functions web.PostData() and web.PostJson() are identical but web.PostJson() function adds "Content-Type", "application/json" header to the request.
You can create your own methods using host.lib() and host.type() helpers as well. They can hook C#/NET methods. Take a look at this topic where we dug into those heplers a bit - Emre explains the difference between the 2 helpers and how to use C# vs. NET …
Confusing at first I know. Look at what Emre says and what Paul did to get it to work …
function sendWebCli(){
var lib = host.lib("System");
var client = new lib.System.Net.WebClient();
// return client.DownloadString('https://www.goggle.com');
client.Headers.Add("Content Type", "application/json");
// client.Headers.Add(<more header stuff like maybe auth>);
var url = "https://api.blah.com/v1/access";
var data = 'some formatted data, like json';
var result = client.UploadString(url,data);
client.Dispose();
}
For example, when we do GQL Auth using jQuery AJAX, we set up authorization parameters in the header and data, like this:
This obviously doesnt work but here is where I am at atm. Is this remotely close you think?
function JSONTest(){
var lib = host.lib("System");
var client = new lib.System.Net.WebClient();
client.Headers.Add("Content Type", "application/json");
client.Headers.Add("Authorization", "Basic AuthKeyblabhlabh");
var url = "https://api.7shifts.com/v1/locations/";
var data = client.DownloadString(url);
return data;
}
It would be simple if they allowed basic auth through url then I could just use the web.Download helper. But apparently they do not allow that.
So it needs to be in the headers somehow.
Here is what I am trying now…I got the headers right from POSTMAN since its working there.
function JSONTest(){
var lib = host.lib("System");
var client = new lib.System.Net.WebClient();
client.Headers.Add("Accept", "application/json");
client.Headers.Add("Glf-Api-Version", "2");
client.Headers.Add("Authorization", "LONGAPIKEYUSERNAMEFORAUTH");
var url = "https://api.7shifts.com/v1/locations/";
var data = client.DownloadString(url);
var json = JSON.parse(data);
return json;
}
Postman says it will automatically generate your headers. So you should be able to look at the Headers tab in Postman to see what it sent and how (the format).
If you look at GQL Auth example with:
Content-Type:application/x-www-form-urlencoded
There is FORM DATA sent like this for example (the last part):
The host.X helpers are very veryvery powerful… they open up endless possibilities because you can work directly with C# / NET Libraries and Assemblies, which gives access to almost anything.