I am currently facing an issue with posting data to elasticsearch from my service. The CORS policy is blocking the action and even after attempting to use a proxy script, the error persists. Can anyone offer assistance in resolving this?
Here is the proxy file content:
{
"/rest/elastic/*": {
"target": "http://xx.xx.xx.xx/",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
This is my service:
import { Injectable } from '@angular/core';
import {HandleError, HttpErrorHandler} from '../http-error-handler.service';
import {HttpClient, HttpHeaders} from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
const userID = '5eada3f00952b44e417fcf82';
@Injectable({
providedIn: 'root'
})
export class PostServiceService {
postElasticUrl = 'http://xx.xx.xx.xx:9200/post/_doc';
private handleError: HandleError;
constructor(
private http: HttpClient,
httpErrorHandler: HttpErrorHandler) {
this.handleError = httpErrorHandler.createHandleError('TimelineService');
}
/** POST Moods to the server */
postMoods(emoji, text) {
this.http.post<any>(this.postElasticUrl, '{"emoji":"' + emoji + '","text":"' + text + '","userID":"' + userID
+ '","timestamp":"' + Date.now() + '" }', httpOptions).subscribe();
}
}
And I am executing the project using:
ng serve --proxy-config proxy.conf.json
The request body is structured as follows:
{emoji: "confused",
text: "dsv",
userID: "5eada3f00952b44e417fcf82",
timeStamp: 1590233638000}