Created
April 27, 2019 03:39
-
-
Save xeniaqian94/559d7016e6cf3a0ce201fc009175a53b to your computer and use it in GitHub Desktop.
A minimum-working-example to post PDF data using axios Javascript, and decode/save the PDF data in python Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fs from "fs"; | |
| // fs.readFile("test.txt", function (err, data) { | |
| // if (err) throw err; | |
| // console.log(data); | |
| // }); | |
| import axios from 'axios'; | |
| import FormData from 'form-data'; | |
| import console = require("console"); | |
| const filePath = 'python-service/tmp/Zhang_et_al-2014-Journal_of_the_Association_for_Information_Science_and_Technology.pdf'; | |
| const imageData = fs.readFileSync(filePath); | |
| const form = new FormData(); | |
| form.append('file', imageData, { | |
| filepath: filePath, | |
| contentType: 'application/pdf', | |
| }); | |
| axios.post('http://localhost:5000/autograb/grobidmetadata', imageData, { | |
| headers: {'Content-Type': 'application/pdf'}, | |
| }).then(response => { | |
| console.log('success!'); | |
| }).catch(err => { | |
| console.log(err); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from flask import Flask, jsonify, request, abort | |
| from flask_cors import CORS | |
| from util import inference | |
| app = Flask(__name__) # creates an app object from Flask. | |
| CORS(app) | |
| @app.route('/autograb/grobidmetadata', methods=['POST']) | |
| def parsePDFAndGROBIDExtract(): | |
| # request.form['file'].read().save('tmp/file-to-save.pdf') | |
| with open('tmp/file_to_save.pdf', 'wb') as f2: | |
| f2.write(request.data) | |
| print("request.form['file']", request.form.get("file")) | |
| print("request.data",request.data) | |
| # request.data.save("tmp/this.jpg") | |
| print("request.file",request.files) | |
| print(request.args) | |
| # print(request.form['file']) | |
| return jsonify({"content":"nothing!"}) | |
| if __name__ == "__main__": | |
| app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment