I am trying to send a pdf file from my Groovy controller to be processed by my Flask end point. I want to encode it to b64 format and decode and access the file at my Flask end point. The pdf file is coming from my gsp file. Any suggestions on how I could do the same?
Groovy controller:
def extractDocument(){
def file = params.filename
if (file instanceof MultipartFile && file != null){
byte[] encoded = Base64.getEncoder().encodeToString(file.getBytes());
println(encoded)
try{
//request_generator(encoded)
HttpURLConnection post = (HttpURLConnection) new URL("http://127.0.0.1:5000/uploadDoc").openConnection();
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorisation", "basic")
//OutputStream
post.getOutputStream().write(encoded)
def postRC = post.getResponseCode()
println(postRC)
if(postRC == 200) {
println(post.getInputStream().getText());
}
else{
println("Not connect")
}
GSP:
<g:uploadForm url= "[controller:'Document', action:'extractDocument']" method="post">
<input type="file" id="myFile" name="filename">
<input type="submit" value="Submit">
</g:uploadForm>
Flask app:
@app.route('/uploadDoc', methods = ['POST', 'GET'])
def upload_file():
print('Hello')
file1 = request.data
file2 = base64.b64decode(file1)
print(file2)
print(file1)
print(request.method)
When trying to send a file I am getting the following results on my Flask app: Hello b'' b'' POST
This will be a tough question to answer as I am not sure how many developers are out there, who speak both Python and Groovy, and maybe use both utilized frameworks.
So, instead of a solution in code, I offer some general debugging techniques which I use when I have such problems.
limit the problem scope
Basically, the problem could be in at least three areas.
I'd try to narrow down the problem.
Depending on your knowledge and preferences...
the Groovy code
Unfortunately, I am no Groovy developer.
You could try to write tests for your code, so you can be sure it is doing what you expect.
Also, I'd probably refactor the code. Your extractDocument
is not only extracting, but doing several things, including converting and sending. This makes it hard to test.
You could read on Single Responsibility Principle
and Dependency Injection
.
the Python code
As above, try to write tests.
Or start a debugger. This can be easily done with inserting import pdb;pdb.set_trace()
on just the line, before it is getting interesting. eg after your print(hello)
.
the network
You could also send some input via curl
directly to your endpoint.
This is maybe not the answer you expected, but when trying to do so, you can find out the problem yourself and also help yourself in future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.