Excuse me if this question sounds beginner or if my terminology is incorrect, because I am new to this.
I have created a basic Python API for reading and writing to a database (CSV file) with Angular 5 as my front end. While I was able to successfully retrieve data from the server, I am encountering issues when trying to post to it. Here is where I think the problem lies:
The method of posting data involves sending an array of JSON objects.
The relevant code snippet is as follows: On the component.ts side:
this._svc.postAPI(this.csvData)
.subscribe(
res => console.log(res),
error => alert('Error!'),
()=> 'Data Submitted to database')
}
On the services.ts side:
postAPI(data): Observable<any>{
return this.http.post(this.urlAPI, data);
}
The error message "405 Method Not Allowed" is displaying, and the Network tab in the console shows:
General
Request URL: http://192.168.0.2:5050/api
Request Method: OPTIONS
Status Code: 405 Method Not Allowed
Remote Address: 192.168.0.2:5050
Referrer Policy: no-referrer-when-downgrade
Response Headers
Allow: POST
Content-Length: 23
Content-Type: text/plain; charset=utf-8
Date: Thu, 12 Jul 2018 03:20:39 GMT
Server: Python/3.5 aiohttp/2.3.10
Request Headers
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: 192.168.0.2:5050
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Postman works fine, so the URL appears correct; however, the issue seems to be related to the data being sent.
I have tried the following approaches:
This did NOT work:
postAPI(data): Observable<any>{
return this.http.post(this.urlAPI, '{}');
}
but this DID work:
postAPI(data): Observable<any>{
return this.http.post(this.urlAPI, '[{}]');
}
and this DID work:
postAPI(data): Observable<any>{
return this.http.post(this.urlAPI, '
[
{
"info": "A",
"dev": "T",
"but": "C",
"msg": "X"
}
]
');
}
I believe the format of the data I'm attempting to post is also in the same JSON array format.
Any help would be greatly appreciated.