Background:
I installed mantisBT 2.5.0 on a test server, enabled the REST API (which is currently in beta phase). After that I generated an API Key and I have tried to make a test HTTP request using the swagger page on /api/rest/swagger. This works fine. (I could only access this page after renaming the .htaccess to _htaccess)
What I want to do:
I want to implement a feature in my app to enable sending "easy" bug reports without visiting mantisBT directly. To test the API I implemented this function, which just calls a "get issue" request. If this works, I can implement a method to create an issue.
Problem:
I can't add the the attribute 'Authorization' with my API token to the HTTP headers of my request. The result is that every time I make the request I get a HTTP Error 401. It seems to be an authorization issue.
Test function:
/**
* function to test the API
* @returns {Observable<Response>}
*/
getIssue(): Observable<Response> {
const api_token = 'XXXXXX';
const params: URLSearchParams = new URLSearchParams();
params.set('id', '1');
const url = 'https://anydomain/mantisbt/api/rest/issues';
const requestOptions = new RequestOptions({
method: RequestMethod.Get,
url: url,
params: params,
headers: new Headers({
'Content-Type': 'application/json',
'Authorization': api_token
})
});
const req = new Request(requestOptions);
return this.http.request(req);
}
...
this.getIssue().subscribe((result)=>{console.log(result)});
Request Header copied from the console (Chrome):
:authority:XXXXXXXX
:method:OPTIONS
:path:/mantisbt/api/rest/issues?id=1
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, sdch, br
accept-language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
access-control-request-headers:authorization,content-type
access-control-request-method:GET
dnt:1
origin:http://localhost
referer:http://localhost/login
user-agent:XXXXXXXX
I think the error is that the request header are not set properly. They shouldn't have the name 'access-control-request-headers' but rather 'Authorization' only, for example. How can I set the headers properly?
EDIT: If I host my app on the same domain like mantisBT it all works fine. I don't understand why. I added header( 'Access-Control-Allow-Origin: *' );
to /api/rest/index.php
EDIT: It seems to be an error on server-side. Now I get this error:
XMLHttpRequest cannot load https://XXXXXX/api/rest/issues?id=1.
Response for preflight has invalid HTTP status code 401
It definitively has something to do with the fact, that the authentification header is not sent properly.