Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.

Moderators: ArturoRobles, ArturoRobles

Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
#813639
Hello,

I'm building a Linux / Python web application that allows users to interact with processmaker cases. Everything has been going great with the REST API until i've tried to upload input documents.

I'm hit with the following error in my response:
Code: Select all
{'code': 400, 'message': 'Bad Request: This filename does not exist!'}
The documentation for this api call recognises this problem with PHP (im using python) and suggests to use CurlFile.
http://wiki.processmaker.com/3.2/REST_A ... _Documents

I've also tried using the "@" before the filename but that didn't work.

I've tried both the standard Python Requests library and httplib2 but neither get around this error.

My code is as follows:
Uploader
Code: Select all
def uploadInputDoc(access_token, app_uid, inp_doc_uid, tas_uid, app_doc_comment, form):
    try:
        url = "http://mybpmurl/api/1.0/workflow/cases/"+app_uid+"/input-document"
        querystring = {"access_token":access_token}
        payload = {'inp_doc_uid':inp_doc_uid,'tas_uid':tas_uid,'app_doc_comment':app_doc_comment,'form':form}
        headers = {
            }
        response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
        return response
    except Exception as e:
        print("error: " + str(e) )
called by:
Code: Select all
uploadInputDoc(bmpAccessToken, "myappidhere", "myinputdocidhere", "mytasuidhere", "comment, "/media/report.pdf" )
Any help would be greatly appreciated!

Thanks,
Daniel
#813650
danielwalters wrote: called by:
Code: Select all
uploadInputDoc(bmpAccessToken, "myappidhere", "myinputdocidhere", "mytasuidhere", "comment, "/media/report.pdf" )
You forgot the closing quotation mark for your comment. Try this:
Code: Select all
uploadInputDoc(bmpAccessToken, "myappidhere", "myinputdocidhere", "mytasuidhere", "comment", "/media/report.pdf" )
You need to put the contents of your file in the POST variables. I don't know if this will work, but try this:
Code: Select all
def uploadInputDoc(access_token, app_uid, inp_doc_uid, tas_uid, app_doc_comment, path_to_file):
    try:
        url = "http://mybpmurl/api/1.0/workflow/cases/"+app_uid+"/input-document"
        querystring = {"access_token":access_token}
        payload = {'inp_doc_uid':inp_doc_uid,'tas_uid':tas_uid,'app_doc_comment':app_doc_comment,'form': open(path_to_file, "rb")}
        headers = {
            }
        response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
        return response
    except Exception as e:
        print("error: " + str(e) )
If that didn't work, then try this:
Code: Select all
        payload = {'inp_doc_uid':inp_doc_uid,'tas_uid':tas_uid,'app_doc_comment':app_doc_comment,'form': open(path_to_file, "rb").read()}
I haven't worked with Python for over a decade, but that is how it used to work.
#813662
Hi amosbatto,

Thanks for the help.

Unfortunately the proposed changes did not work, and results with the same error.
Code: Select all
def uploadInputDoc(access_token, app_uid, inp_doc_uid, tas_uid, app_doc_comment, path_to_file):
    try:
        url = "http://mybpmurl/api/1.0/workflow/cases/"+app_uid+"/input-document"
        querystring = {"access_token":access_token}
        payload = {'inp_doc_uid':inp_doc_uid,'tas_uid':tas_uid,'app_doc_comment':app_doc_comment,'form': open(path_to_file, "rw").read()}
        headers = {
            }
        response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
        return response
    except Exception as e:
        print("error: " + str(e) )
returns
Code: Select all
{'error': {'code': 400, 'message': 'Bad Request: This filename does not exist!'}}
#813668
The request method i'm using is documented here:
http://docs.python-requests.org/en/mast ... ts.request

I've read that the data is 'form-encoded', but thats about it - I don't currently understand the differences or know how they work.

Is there a certain type of encoding that is known to work that I could try and adopt? If so, i'm happy to go away to learn this.

Thanks again for the help. Luckily I have GDrive and Django as additional storage platforms if this fails, bur really it would be great to integrate all parts of my system with ProcessMaker.

Daniel
#813686
"form encoding" probably means that the data is being passed through urllib.urlencode().

Does it work this way?:
Code: Select all
def uploadInputDoc(access_token, app_uid, inp_doc_uid, tas_uid, app_doc_comment, path_to_file):
    try:
        url = "http://mybpmurl/api/1.0/workflow/cases/"+app_uid+"/input-document"
        querystring = {"access_token":access_token}
        payload = {'inp_doc_uid':inp_doc_uid,'tas_uid':tas_uid,'app_doc_comment':app_doc_comment}
        files = {'form': open(path_to_file, "rb")}
        headers = {
            }
        response = requests.request("POST", url, data=payload, headers=headers, params=querystring, files=files)
        return response
    except Exception as e:
        print("error: " + str(e) ) 
#823477
jess10 wrote:I upload file via Postman is not working but Advanced Rest Client is working, please help me.
The only way to upload a file in Postman is to encode the file in Base64.

Here is an online Base64 encoder:
https://codebeautify.org/base64-encode

To avoid using Base64, it is recommended to use a language like PHP or Python to upload files.

Hello My friends, I have a problem and I can not s[…]

I am so happy that I found https://essaywriter.org[…]

Hi, Is there any method, e.g. by trigger, during […]

Hi, Please check the following link: http://pmle[…]