How to easy API Call

Post Reply
User avatar
bkromer
Member
Posts: 39
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

How to easy API Call

Post by bkromer » Wed Oct 09, 2019 3:13 pm

I want to do a GET http request with a Bearer token:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var theHTTP = new HTTP( HTTP.SSL );
	theHTTP.authScheme = HTTP.OauthAuth;
	theHTTP.authorization = "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";	
	theHTTP.url = "https://app.keyline-mis.com/api/v2/sales/orders";
	theHTTP.get();
	
	 while( !theHTTP.waitForFinished( 3 ) )
  {
       job.log( 1, "API CALL IN PROGRESS!" );
  } 
	
	if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
  {
		
       
		job.log(1,"RESPONSE: ",theHTTP.getServerResponse().toString( "UTF-8" ));
  }
	
	
}
In messages i get: "09.10.19 15:10,Debug,Control,,,,,HTTP GET request error: HTTP protocol error. 406 Not Acceptable." and "09.10.19 15:10,Info,Scripter control,Test Flow,Script Element,,TDB_Inapa_Clear.pdf,Script evaluation message: End jobArrived entry point with the test fixture"

Docs: https://keyline-mis.de/docs/api#

How can i make this work?
lg
Ben
Benjamin Kromer

User avatar
bkromer
Member
Posts: 39
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: How to easy API Call

Post by bkromer » Fri Jul 10, 2020 1:11 pm

I think I need to add a header with the bearer key instead of the theHTTP.authorization() method.
In there Docs they say:
"...
Keyline requires API users to authenticate before they can access any information, and leverages a bearer token based authentication scheme to secure access.

All valid requests to the Keyline API must therefore contain the Authorization header field, which contains a valid API key, prefixed with the string Bearer, so that the header line looks similar to the following:

Authorization: Bearer f338bc59b1ee082ee08600f85022cf7d661426ee342b22c73c18d3875f2e

......
But when I try this I get an error:
Error in line 46 of script : No matching slot found_ available overloads are:; void SHTTP::addHeader(QString_QString);

Code: Select all

var theHTTP = new HTTP( HTTP.SSL );
		theHTTP.authScheme = HTTP.OauthAuth;
		theHTTP.addHeader( '  "Authorization", "Bearer 6666666666666666666666666666666"  ' );
		theHTTP.url = "https://app.keyline-mis.com/api/v2/customer_relations/organizations";
		theHTTP.get();
		while (!theHTTP.waitForFinished(3)) {
			job.log(1, 'API CALL IN PROGRESS!');
		}
	
		if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
			job.log(1, 'API CALL successfull RESPONSE: ' + theHTTP.getServerResponse().toString('UTF-8'));
			
		} else {
			job.log(
				3,
				'API CALL Failed:  ' +
					theHTTP.getServerResponse().toString('UTF-8') +
					'  Status Code: ' +
					theHTTP.statusCode.toString('UTF-8')
			);
		}
Benjamin Kromer

Padawan
Advanced member
Posts: 264
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: How to easy API Call

Post by Padawan » Fri Jul 10, 2020 2:30 pm

Can you try this?

Code: Select all

	theHTTP.authScheme = HTTP.ProprietaryAuth;
	theHTTP.addHeader( "Authorization", "Bearer 6666666666666666666666666666666");

When you get this error:
No matching slot found_ available overloads are

Then you are not providing the right amount of arguments in the function. In this case the header name and value should be separate arguments.

User avatar
bkromer
Member
Posts: 39
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: How to easy API Call

Post by bkromer » Fri Jul 10, 2020 2:49 pm

Thank you.

Now it looks different.
The error I get now is:
HTTP GET request error: HTTP protocol error. 406 Not Acceptable.

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var theHTTP = new HTTP( HTTP.SSL );
		theHTTP.authScheme = HTTP.ProprietaryAuth;
		theHTTP.addHeader( "Authorization", "Bearer 1234");
		theHTTP.url = "https://app.keyline-mis.com/api/v2/customer_relations/organizations";
		theHTTP.get();
		while (!theHTTP.waitForFinished(3)) {
			job.log(1, 'API CALL IN PROGRESS!');
		}
	
		if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
			job.log(1, 'API CALL successfull RESPONSE: ' + theHTTP.getServerResponse().toString('UTF-8'));
			
		} else {
			job.log(
				3,
				'API CALL Failed:  ' +
					theHTTP.getServerResponse().toString('UTF-8') +
					'  Status Code: ' +
					theHTTP.statusCode.toString('UTF-8')
			);
		}
}
Benjamin Kromer

User avatar
bkromer
Member
Posts: 39
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: How to easy API Call

Post by bkromer » Fri Jul 10, 2020 3:35 pm

Now it works, had to add another Header there. :D
"Accept":"application/json"
Thanks a lot!

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var theHTTP = new HTTP( HTTP.SSL );
		theHTTP.authScheme = HTTP.ProprietaryAuth;
		theHTTP.addHeader( "Authorization", "Bearer 123456");
		theHTTP.addHeader("Accept","application/json");
		theHTTP.url = "https://app.keyline-mis.com/api/v2/customer_relations/organizations";
		theHTTP.get();
		while (!theHTTP.waitForFinished(3)) {
			job.log(1, 'API CALL IN PROGRESS!');
		}
	
		if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
			job.log(1, 'API CALL successfull RESPONSE: ' + theHTTP.getServerResponse().toString('UTF-8'));
			
		} else {
			job.log(
				3,
				'API CALL Failed:  ' +
					theHTTP.getServerResponse().toString('UTF-8') +
					'  Status Code: ' +
					theHTTP.statusCode.toString('UTF-8')
			);
		}
}
Benjamin Kromer

Post Reply