0

**I am creating azure python function which will eventually create kml file with some information and now when i call the function URL with post method it should download me the file **

I have written simple code but its downloading anything

file_name = "PIG_test.kml"
lambda_path = file_name
f = open(lambda_path,'w')
#here will do some preprocessing which will 
f.write(footer)
f.close()
with open(lambda_path, "rb") as f:
    b = base64.b64encode(f.read()).decode("utf-8")

can anyone please answer this ?

vishal
  • 209
  • 2
  • 4
  • 14
  • https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=azurecli-linux%2Capplication-level have you gone through this link – SaiSakethGuduru Dec 29 '21 at 09:11
  • yes but didnt find any relevant solution – vishal Dec 29 '21 at 09:39
  • when you make HTTP POST call to your function, what does it return? are you returning the response body that contains your file content? are you returning proper mime type ? show us the code snippet where you do `return func.HttpResponse( body = b, ...` – Anand Sowmithiran Dec 29 '21 at 11:39
  • refer this [page](https://learn.microsoft.com/en-us/python/api/azure-functions/azure.functions.httpresponse?view=azure-python) to properly fill the `body`, headers like `Content-Length` and the MIME type of your http `response`. – Anand Sowmithiran Dec 29 '21 at 11:42

1 Answers1

0

The HttpResponse method consist of below Arguments which we cannot add any instructions on post method

HttpResponse(body=None, *, status_code=None, headers=None, mimetype=None, charset=None)

Instead of that, you can use the below steps to achieve this

data = func.HttpResponse("Hello, {name}. This HTTP triggered function executed successfully." + str( lines) )
with open(file_dwn_name,'w') as f:
f.write(str(data.__body))
f.close()
with open(file_dwn_name) as f:
lines = f.readlines()
return data

You can store the response information in variable and use it. And you can try to create one file and add that information in file. In this way only you can save and store the response data in azure function.

enter image description here

We store this information in file

enter image description here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15