I am trying to pass the path of an xml file to a flask/Python based REST-microservice. When passing the url manually to the service, it works as intended. However, I have problems calling the service from my laravel project.
Call to python rest service from laravel:
function gpxToJson(){
url = "http://127.0.0.1:5000/convert?path=C:/xampp/htdocs/greenlaneAdventures/public/storage/gpx/OOCe4r5Mas8w94uGgEb8qYlI0T3ClZDoclcfrR7s.xml" ;
fetch(url).then(function (response){
var track = JSON.parse(response.json());
return track;
});
}
Python script:
import gpxpy
import pandas as pd
from flask_cors import CORS
from flask import Flask, request, make_response, render_template
from flask_restful import Resource, Api
# create empty web-application
app = Flask(__name__)
api = Api(app)
CORS(app)
# create necessary classes
class Converter(Resource):
def get(self):
# path passed as argument
file = request.args.get('path', '')
# parse gpx file
gpx = gpxpy.parse(open(file))
# define data
track = gpx.tracks[0]
segment = track.segments[0]
# load wanted data into a panda dataframe
data = []
segment_length = segment.length_3d()
for point_idx, point in enumerate(segment.points):
data.append([point.longitude, point.latitude, point.elevation,
point.time, segment.get_speed(point_idx)])
columns = ['Longitude', 'Latitude', 'Altitude', 'Time', 'Speed']
gpx_df = pd.DataFrame(data, columns=columns)
# convert dataframe to json
gpx_json = gpx_df.to_json(orient='index')
print(gpx_json)
#return the converted json (http response code 200 = OK. The request has suceeded)
headers = {"Content-Type": "application/json"}
return make_response(gpx_json, 200, headers)
api.add_resource(Converter, "/convert")
#error handling
@app.errorhandler(404)
def not_found():
"""Page not found."""
return make_response(
render_template("404.html"),
404
)
@app.errorhandler(400)
def bad_request():
"""Bad request."""
return make_response(
render_template("400.html"),
400
)
@app.errorhandler(500)
def server_error():
"""Internal server error."""
return make_response(
render_template("500.html"),
500
)
#main function call
if __name__ == "__main__":
app.run(debug=True)
When calling the function gpxToJson() by submitting a form, I get the following error message in firefox.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…