Retrieving value of options flags from curl command in Flask - python

I'm sending a curl command to a HTTP Flask server I've created. The command I'm sending is as follows:
curl -X POST --data-binary #africa-toto.wav http://localhost:5000/
Is there a way to parse the actual name of the file from this command using Flask (i.e. "africa-toto.wav")? I can receive the binary data just fine, but I'm not able to find the actual filename referenced in any of the attributes of Flask's request object
I have tried checking request.files, and a few others, but no filenames seem to be getting stored anywhere

Related

Upload secure files to GitLab using requests python module

I'm trying to upload a secure file to my repository in GitLab.
While I am able to upload a secure file with curl, I encounter an error when using requests in Python.
my python code:
r = requests.post("https://gitlab.com/api/v4/projects/10186699/secure_files",
headers={"PRIVATE-TOKEN": "glpat-TH7FM3nThKmHgOp"},
files={"file": open("/Users/me/Desktop/dev/web-server/utils/a.txt", "r"),
"name": "a.txt"})
print(r.status_code,r.json())
Response:
400 {'error': 'name is invalid'}
The equivalent curl command I use that actually works:
curl --request POST --header "PRIVATE-TOKEN: glpat-TH7FM3nThKmHgOp" https://gitlab.com/api/v4/projects/10186699/secure_files --form "name=a.txt" --form "file=#/Users/me/Desktop/dev/web-server/utils/a.txt"
The equivalent call will be
import requests
resp = requests.post(
"https://gitlab.com/api/v4/projects/10186699/secure_files",
headers={"PRIVATE-TOKEN": "glpat-TH7FM3nThKmHgOp"},
files={"file": open("/Users/me/Desktop/dev/web-server/utils/a.txt", "rb")},
data={"name": "a.txt"}
)
print(resp.status_code,resp.json())
This is because the file= parameter is intended only for uploading files. On the other hand, name is your form data (you need to pass in the data= parameter).
It's also recommended to open files in binary mode. (docs)

sync call using request libraries instead curl

I have a code in python:
cmd = "curl -d 'protection={protection}&Code={Code}' -X POST https://example.com/web/services/toaf6.php"
os.system(cmd.format(protection=protection, Code=Code))
I want to transform this so it uses pythons request libraries???
This request should be sync, but endpoint will return immediately response and I will store transaction ID so I can ask some other endpoint for job status.

How to create a post request in flask using python to create html mail body and its corresponding curl command?

Need to create an email with the different mail components like send_from, send_to, attachments and html_body using python and flask. I need the exact curl command to do the same. Tried the following curl which gives errors:
curl -F 'files=#/tmp/holiday.png' -F 'files=#/home/user1/sample.html' -F metadata="{'send_from':'xyz#domain.com'}" http://localhost:8085/mail
I believe this is a form data POST request method. Is there something wrong in this curl? How do I parse the content of the file to create html body?

Cannot access the request json_body when using Chalice

I'm attempting to make a curl request to my python api that is using the AWS package Chalice.
When I try to access the app.current_request.json_body a JSON Parse error is thrown. Cannot figure out why this is happening. My JSON is formatted properly as far as I can tell.
Here is the curl request:
(echo -n '{"data": "test"}') |
curl -H "Content-Type: application/json" -d #- $URL
Here is the python Chalice code:
app = Chalice(app_name='predictor')
#app.route('/', methods=['POST'], content_types=['application/json'])
def index():
try:
body = app.current_request.json_body
except Exception as e:
return {'error': str(e)}
When I invoke the route using the above curl request I get the following error:
{"error": "BadRequestError: Error Parsing JSON"}
Note: When I remove the .json_body from the app.current_request. I no longer get the error.
Any thoughts?
The documentation indeed indicates that the problem is Content-Type:
The default behavior of a view function supports a request body of application/json. When a request is made with a Content-Type of application/json, the app.current_request.json_body attribute is automatically set for you. This value is the parsed JSON body.
You can also configure a view function to support other content types. You can do this by specifying the content_types parameter value to your app.route function. This parameter is a list of acceptable content types.
It suggests that changing the Content-Type might make json_body work, but I didn't manage to have any success with it.
However using app.current_request.raw_body.decode() instead of app.current_request.json_body solves the problem.

Curl: HTTP/1.1 100 Continue message on File Upload

I'm uploading a file using curl in one of my django project, it works fine in localhost, but when I hosted the project in remote server, it does not work.
I'm sending the file from this command
curl -i --form docfile=#/path_to_file http://example.com/process-file
and in views.py, I'm handling this file as
def process_file(request):
if request.method != 'POST':
return HttpResponseNotAllowed('Only POST Method')
docfile = request.FILES['docfile']
output = main(0, docfile) # output is json object
return HttpResponse(output,content_type = "application/json")
This works perfectly fine when i run in local machine, but sending POST request to remote server with curl returns
HTTP/1.1 100 Continue
and do nothing. File is getting uploaded. What should I do.
Thanks
Edit 1:
I tried to send some other HttpResponse (file name) from other view method, its working fine, but when i process file, it just sends HTTP/1.1 100 Continue
I don't know exactly how to fix this in your project, but it looks like your curl is sending the Expect: 100-continue header, prompting the remote server to send back HTTP/1.1 Continue. curl waits to get back that response then uploads the form. You say curl returns the HTTP/1.1 100 Continue then "does nothing," but it should actually be uploading after it receives that, so maybe whatever callback you're using isn't returning that. I would try looking at it with Wireshark.

Categories

Resources