Visual Process API error

Leonid
Leonid New Altair Community Member
edited December 2020 in Community Q&A
Hello,

I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?

Thank you!

monarch_rest_api.py
import requests

url = "http://us1-monarch1/MSAdmin/api"
login_info = {"Username":"test_login", "Password":"test_password"}
jobname = "Test Job"

resp = requests.post(url + "/login", json = login_info)
print ("Login: ", resp.status_code)

resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})
print ("Start: ", resp.status_code)
print (resp.json())

Output
H:\python>python monarch_rest_api.py
Login: 200
Start: 500
{'Message': 'An error has occurred.'}

------------------------------
Leonid Shteyngardt
App Dev Manager
Canaccord Genuity Inc.
New York NY
(212) 389-8176
------------------------------
Tagged:

Best Answer

  • CPorthouse
    CPorthouse
    Altair Employee
    edited December 2020 Answer ✓
    I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:
    def getResponse(url, jsonData):     global session     if not 'session' in globals():         session = requests.Session()         session.headers.update({'Content-Type':'application/json'})      r = session.post(url, json=jsonData)     return r


    So when I login (with a defined function I created called 'login'):
    response = login('admin', 'password')
    I create a global "session" variable which will be used automatically when I call my getResponse function:
    def startProcess(processName):     start_url = 'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'     print('Attempting to start process ' + processName)     return getResponse(start_url, processName)
    ------------------------------
    Chris Porthouse
    Senior Implementation & Integration Engineer
    Altair
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 05-09-2020 04:55 PM
    From: Leonid Shteyngardt
    Subject: Visual Process API error

    Hello,

    I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?

    Thank you!

    monarch_rest_api.py
    import requests

    url = "http://us1-monarch1/MSAdmin/api"
    login_info = {"Username":"test_login", "Password":"test_password"}
    jobname = "Test Job"

    resp = requests.post(url + "/login", json = login_info)
    print ("Login: ", resp.status_code)

    resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})
    print ("Start: ", resp.status_code)
    print (resp.json())

    Output
    H:\python>python monarch_rest_api.py
    Login: 200
    Start: 500
    {'Message': 'An error has occurred.'}

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------"

Answers

  • Mahmoud
    Mahmoud
    Altair Employee
    edited May 2020
    Hi Leonid,
    I am not familiar with Python syntax, but you need to create a session variable for the first request and then use the session variable for subsequent request.  In the python syntax you provided I don't see you are passing a session variable to the second request.

    The following is the syntax in PowerShell script:
    $rep = Invoke-WebRequest -Uri $login_url -Method POST -Body $postParams -SessionVariable session

    Noticed the session object in the invoke-WebRequest method.
    The session object is then used in the next invoke-web-request method.
    Invoke-WebRequest -Uri $start_process_url -Method POST -Body $processName -WebSession $session

    Regards
    Mo

    ------------------------------
    Mahmoud Abdolrahim
    Senior Implementation & Integration Engineer
    Datawatch Corporation
    MA
    (978) 935-3840
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 05-09-2020 04:55 PM
    From: Leonid Shteyngardt
    Subject: Visual Process API error

    Hello,

    I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?

    Thank you!

    monarch_rest_api.py
    import requests

    url = "http://us1-monarch1/MSAdmin/api"
    login_info = {"Username":"test_login", "Password":"test_password"}
    jobname = "Test Job"

    resp = requests.post(url + "/login", json = login_info)
    print ("Login: ", resp.status_code)

    resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})
    print ("Start: ", resp.status_code)
    print (resp.json())

    Output
    H:\python>python monarch_rest_api.py
    Login: 200
    Start: 500
    {'Message': 'An error has occurred.'}

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------
    "
  • CPorthouse
    CPorthouse
    Altair Employee
    edited December 2020 Answer ✓
    I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:
    def getResponse(url, jsonData):     global session     if not 'session' in globals():         session = requests.Session()         session.headers.update({'Content-Type':'application/json'})      r = session.post(url, json=jsonData)     return r


    So when I login (with a defined function I created called 'login'):
    response = login('admin', 'password')
    I create a global "session" variable which will be used automatically when I call my getResponse function:
    def startProcess(processName):     start_url = 'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'     print('Attempting to start process ' + processName)     return getResponse(start_url, processName)
    ------------------------------
    Chris Porthouse
    Senior Implementation & Integration Engineer
    Altair
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 05-09-2020 04:55 PM
    From: Leonid Shteyngardt
    Subject: Visual Process API error

    Hello,

    I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?

    Thank you!

    monarch_rest_api.py
    import requests

    url = "http://us1-monarch1/MSAdmin/api"
    login_info = {"Username":"test_login", "Password":"test_password"}
    jobname = "Test Job"

    resp = requests.post(url + "/login", json = login_info)
    print ("Login: ", resp.status_code)

    resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})
    print ("Start: ", resp.status_code)
    print (resp.json())

    Output
    H:\python>python monarch_rest_api.py
    Login: 200
    Start: 500
    {'Message': 'An error has occurred.'}

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------"
  • Leonid
    Leonid New Altair Community Member
    edited May 2020

    I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:

    def getResponse(url, jsonData):     global session     if not 'session' in globals():         session = requests.Session()         session.headers.update({'Content-Type':'application/json'})      r = session.post(url, json=jsonData)     return r
    </code><br /><br />So when I login (with a defined function I created called 'login'):<br /> <pre class="language-python">response <span class="token operator">=</span> login<span class="token punctuation">(</span><span class="token string">'admin'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">)</span></pre> <br />I create a global "session" variable which will be used automatically when I call my getResponse function:<br /> <pre class="language-python"><span class="token keyword">def</span> <span class="token function">startProcess</span><span class="token punctuation">(</span>processName<span class="token punctuation">)</span><span class="token punctuation">:</span> start_url <span class="token operator">=</span> <span class="token string">'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Attempting to start process '</span> <span class="token operator">+</span> processName<span class="token punctuation">)</span> <span class="token keyword">return</span> getResponse<span class="token punctuation">(</span>start_url<span class="token punctuation">,</span> processName<span class="token punctuation">)</span></pre> <br /><br />------------------------------<br />Chris Porthouse<br />Senior Implementation & Integration Engineer<br />Altair<br />------------------------------<br />-------------------------------------------<br />Original Message:<br />Sent: 05-09-2020 04:55 PM<br />From: Leonid Shteyngardt<br />Subject: Visual Process API error<br /><br />Hello,<br /><br />I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?<br /><br />Thank you!<br /><br /><span style="text-decoration: underline;"><strong>monarch_rest_api.py</strong></span><br />import requests<br /><br />url = "http://us1-monarch1/MSAdmin/api"<br />login_info = {"Username":"test_login", "Password":"test_password"}<br />jobname = "Test Job"<br /><br />resp = requests.post(url + "/login", json = login_info)<br />print ("Login: ", resp.status_code)<br /><br />resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})<br />print ("Start: ", resp.status_code)<br />print (resp.json())<br /><br /><span style="text-decoration: underline;"><strong>Output</strong></span><br />H:\python>python monarch_rest_api.py<br />Login: 200<br />Start: 500<br />{'Message': 'An error has occurred.'}<br /><br />------------------------------<br />Leonid Shteyngardt<br />App Dev Manager<br />Canaccord Genuity Inc.<br />New York NY<br />(212) 389-8176<br />------------------------------"</p></p></div></blockquote> Thank you very much Chris and Mo for your suggestions and code samples! You not only saved me time and frustration, but also provided the way to write concise and efficient code. Everything works correctly now.  I just have one more question. <br />When I try to get process status:<br /><br />response = session.get(url + "/v1/visualprocesses/" + tracking_id + "/status")<br />print (response.text)<br /><br />the <strong>response.text</strong> returns codes - 200, 300, 400. But when I try to run the same request in the Chrome it returns XML and descriptions: Ready, Running, Completed. Is there a way to get the same description from within the Python?<br /><br />Thank you very much,<br />Leonid<br><br>------------------------------<br>Leonid Shteyngardt<br>App Dev Manager<br>Canaccord Genuity Inc.<br>New York NY<br>(212) 389-8176<br>------------------------------<br>-------------------------------------------<br>Original Message:<br>Sent: 05-10-2020 10:24 AM<br>From: Chris Porthouse<br>Subject: Visual Process API error<br><br>I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:<br> <pre class="language-python"><span class="token keyword">def</span> <span class="token function">getResponse</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> jsonData<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">global</span> session <span class="token keyword">if</span> <span class="token operator">not</span> <span class="token string">'session'</span> <span class="token keyword">in</span> globals<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> session <span class="token operator">=</span> requests<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span> session<span class="token punctuation">.</span>headers<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'Content-Type'</span><span class="token punctuation">:</span><span class="token string">'application/json'</span><span class="token punctuation">}</span><span class="token punctuation">)</span> r <span class="token operator">=</span> session<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> json<span class="token operator">=</span>jsonData<span class="token punctuation">)</span> <span class="token keyword">return</span> r</pre> <code>

    So when I login (with a defined function I created called 'login'):
    response = login('admin', 'password')
    I create a global "session" variable which will be used automatically when I call my getResponse function:
    def startProcess(processName):     start_url = 'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'     print('Attempting to start process ' + processName)     return getResponse(start_url, processName)
    ------------------------------
    Chris Porthouse
    Senior Implementation & Integration Engineer
    Altair
    ------------------------------

    Original Message:
    Sent: 05-09-2020 04:55 PM
    From: Leonid Shteyngardt
    Subject: Visual Process API error

    Hello,

    I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?

    Thank you!

    monarch_rest_api.py
    import requests

    url = "http://us1-monarch1/MSAdmin/api"
    login_info = {"Username":"test_login", "Password":"test_password"}
    jobname = "Test Job"

    resp = requests.post(url + "/login", json = login_info)
    print ("Login: ", resp.status_code)

    resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})
    print ("Start: ", resp.status_code)
    print (resp.json())

    Output
    H:\python>python monarch_rest_api.py
    Login: 200
    Start: 500
    {'Message': 'An error has occurred.'}

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------"
  • CPorthouse
    CPorthouse
    Altair Employee
    edited December 2020
    Leonid said:

    Thank you very much Chris and Mo for your suggestions and code samples! You not only saved me time and frustration, but also provided the way to write concise and efficient code. Everything works correctly now.  I just have one more question.
    When I try to get process status:

    response = session.get(url + "/v1/visualprocesses/" + tracking_id + "/status")
    print (response.text)

    the response.text returns codes - 200, 300, 400. But when I try to run the same request in the Chrome it returns XML and descriptions: Ready, Running, Completed. Is there a way to get the same description from within the Python?

    Thank you very much,
    Leonid

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 05-10-2020 10:24 AM
    From: Chris Porthouse
    Subject: Visual Process API error

    I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:

    def getResponse(url, jsonData):     global session     if not 'session' in globals():         session = requests.Session()         session.headers.update({'Content-Type':'application/json'})      r = session.post(url, json=jsonData)     return r
    </code><br><br>So when I login (with a defined function I created called 'login'):<br> <pre class="language-python">response <span class="token operator">=</span> login<span class="token punctuation">(</span><span class="token string">'admin'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">)</span></pre><br>I create a global "session" variable which will be used automatically when I call my getResponse function:<br> <pre class="language-python"><span class="token keyword">def</span> <span class="token function">startProcess</span><span class="token punctuation">(</span>processName<span class="token punctuation">)</span><span class="token punctuation">:</span> start_url <span class="token operator">=</span> <span class="token string">'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Attempting to start process '</span> <span class="token operator">+</span> processName<span class="token punctuation">)</span> <span class="token keyword">return</span> getResponse<span class="token punctuation">(</span>start_url<span class="token punctuation">,</span> processName<span class="token punctuation">)</span></pre><br><br>------------------------------<br>Chris Porthouse<br>Senior Implementation & Integration Engineer<br>Altair<br>------------------------------<br><br>Original Message:<br>Sent: 05-09-2020 04:55 PM<br>From: Leonid Shteyngardt<br>Subject: Visual Process API error<br><br>Hello,<br><br>I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?<br><br>Thank you!<br><br><span style="text-decoration: underline;"><strong>monarch_rest_api.py</strong></span><br>import requests<br><br>url = "http://us1-monarch1/MSAdmin/api"<br>login_info = {"Username":"test_login", "Password":"test_password"}<br>jobname = "Test Job"<br><br>resp = requests.post(url + "/login", json = login_info)<br>print ("Login: ", resp.status_code)<br><br>resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})<br>print ("Start: ", resp.status_code)<br>print (resp.json())<br><br><span style="text-decoration: underline;"><strong>Output</strong></span><br>H:\python>python monarch_rest_api.py<br>Login: 200<br>Start: 500<br>{'Message': 'An error has occurred.'}<br><br>------------------------------<br>Leonid Shteyngardt<br>App Dev Manager<br>Canaccord Genuity Inc.<br>New York NY<br>(212) 389-8176<br>------------------------------"</p></p></div></blockquote> The response.text method will return the content of the response.  Different browsers will handle it differently.  If you only want the text of the response try<br /> <pre class="language-python">response<span class="token punctuation">.</span>reason</pre> <p><br />If you simply want the status code you can use:</p> <pre class="language-python">response<span class="token punctuation">.</span>status_code</pre> <p><br />status_code also has an overloaded __bool__() operator so you can use an if statement and if the status_code is between 200-400, it will return TRUE.</p><br><br>------------------------------<br>Chris Porthouse<br>Senior Implementation & Integration Engineer<br>Altair<br>------------------------------<br>-------------------------------------------<br>Original Message:<br>Sent: 05-10-2020 07:30 PM<br>From: Leonid Shteyngardt<br>Subject: Visual Process API error<br><br>Thank you very much Chris and Mo for your suggestions and code samples! You not only saved me time and frustration, but also provided the way to write concise and efficient code. Everything works correctly now.  I just have one more question.<br>When I try to get process status:<br><br>response = session.get(url + "/v1/visualprocesses/" + tracking_id + "/status")<br>print (response.text)<br><br>the <strong>response.text</strong> returns codes - 200, 300, 400. But when I try to run the same request in the Chrome it returns XML and descriptions: Ready, Running, Completed. Is there a way to get the same description from within the Python?<br><br>Thank you very much,<br>Leonid<br><br>------------------------------<br>Leonid Shteyngardt<br>App Dev Manager<br>Canaccord Genuity Inc.<br>New York NY<br>(212) 389-8176<br>------------------------------<br><br>Original Message:<br>Sent: 05-10-2020 10:24 AM<br>From: Chris Porthouse<br>Subject: Visual Process API error<br><br>I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:<br> <pre class="language-python"><span class="token keyword">def</span> <span class="token function">getResponse</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> jsonData<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">global</span> session <span class="token keyword">if</span> <span class="token operator">not</span> <span class="token string">'session'</span> <span class="token keyword">in</span> globals<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> session <span class="token operator">=</span> requests<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span> session<span class="token punctuation">.</span>headers<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'Content-Type'</span><span class="token punctuation">:</span><span class="token string">'application/json'</span><span class="token punctuation">}</span><span class="token punctuation">)</span> r <span class="token operator">=</span> session<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> json<span class="token operator">=</span>jsonData<span class="token punctuation">)</span> <span class="token keyword">return</span> r</pre> <code>

    So when I login (with a defined function I created called 'login'):
    response = login('admin', 'password')
    I create a global "session" variable which will be used automatically when I call my getResponse function:
    def startProcess(processName):     start_url = 'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'     print('Attempting to start process ' + processName)     return getResponse(start_url, processName)
    ------------------------------
    Chris Porthouse
    Senior Implementation & Integration Engineer
    Altair

    Original Message:
    Sent: 05-09-2020 04:55 PM
    From: Leonid Shteyngardt
    Subject: Visual Process API error

    Hello,

    I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?

    Thank you!

    monarch_rest_api.py
    import requests

    url = "http://us1-monarch1/MSAdmin/api"
    login_info = {"Username":"test_login", "Password":"test_password"}
    jobname = "Test Job"

    resp = requests.post(url + "/login", json = login_info)
    print ("Login: ", resp.status_code)

    resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})
    print ("Start: ", resp.status_code)
    print (resp.json())

    Output
    H:\python>python monarch_rest_api.py
    Login: 200
    Start: 500
    {'Message': 'An error has occurred.'}

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------"
  • Leonid
    Leonid New Altair Community Member
    edited May 2020

    The response.text method will return the content of the response.  Different browsers will handle it differently.  If you only want the text of the response try

    response.reason


    If you simply want the status code you can use:

    response.status_code


    status_code also has an overloaded __bool__() operator so you can use an if statement and if the status_code is between 200-400, it will return TRUE.

    ------------------------------
    Chris Porthouse
    Senior Implementation & Integration Engineer
    Altair
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 05-10-2020 07:30 PM
    From: Leonid Shteyngardt
    Subject: Visual Process API error

    Thank you very much Chris and Mo for your suggestions and code samples! You not only saved me time and frustration, but also provided the way to write concise and efficient code. Everything works correctly now.  I just have one more question.
    When I try to get process status:

    response = session.get(url + "/v1/visualprocesses/" + tracking_id + "/status")
    print (response.text)

    the response.text returns codes - 200, 300, 400. But when I try to run the same request in the Chrome it returns XML and descriptions: Ready, Running, Completed. Is there a way to get the same description from within the Python?

    Thank you very much,
    Leonid

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------

    Original Message:
    Sent: 05-10-2020 10:24 AM
    From: Chris Porthouse
    Subject: Visual Process API error

    I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:
    def getResponse(url, jsonData):     global session     if not 'session' in globals():         session = requests.Session()         session.headers.update({'Content-Type':'application/json'})      r = session.post(url, json=jsonData)     return r
    </code><br><br>So when I login (with a defined function I created called 'login'):<br> <pre class="language-python">response <span class="token operator">=</span> login<span class="token punctuation">(</span><span class="token string">'admin'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">)</span></pre><br>I create a global "session" variable which will be used automatically when I call my getResponse function:<br> <pre class="language-python"><span class="token keyword">def</span> <span class="token function">startProcess</span><span class="token punctuation">(</span>processName<span class="token punctuation">)</span><span class="token punctuation">:</span> start_url <span class="token operator">=</span> <span class="token string">'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Attempting to start process '</span> <span class="token operator">+</span> processName<span class="token punctuation">)</span> <span class="token keyword">return</span> getResponse<span class="token punctuation">(</span>start_url<span class="token punctuation">,</span> processName<span class="token punctuation">)</span></pre><br><br>------------------------------<br>Chris Porthouse<br>Senior Implementation & Integration Engineer<br>Altair<br><br>Original Message:<br>Sent: 05-09-2020 04:55 PM<br>From: Leonid Shteyngardt<br>Subject: Visual Process API error<br><br>Hello,<br><br>I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?<br><br>Thank you!<br><br><span style="text-decoration: underline;"><strong>monarch_rest_api.py</strong></span><br>import requests<br><br>url = "http://us1-monarch1/MSAdmin/api"<br>login_info = {"Username":"test_login", "Password":"test_password"}<br>jobname = "Test Job"<br><br>resp = requests.post(url + "/login", json = login_info)<br>print ("Login: ", resp.status_code)<br><br>resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})<br>print ("Start: ", resp.status_code)<br>print (resp.json())<br><br><span style="text-decoration: underline;"><strong>Output</strong></span><br>H:\python>python monarch_rest_api.py<br>Login: 200<br>Start: 500<br>{'Message': 'An error has occurred.'}<br><br>------------------------------<br>Leonid Shteyngardt<br>App Dev Manager<br>Canaccord Genuity Inc.<br>New York NY<br>(212) 389-8176<br>------------------------------"</p></p></div></blockquote> Works perfectly. Thank you so much, Chris!<br><br>------------------------------<br>Leonid Shteyngardt<br>App Dev Manager<br>Canaccord Genuity Inc.<br>New York NY<br>(212) 389-8176<br>------------------------------<br>-------------------------------------------<br>Original Message:<br>Sent: 05-11-2020 07:16 AM<br>From: Chris Porthouse<br>Subject: Visual Process API error<br><br>The response.text method will return the content of the response.  Different browsers will handle it differently.  If you only want the text of the response try<br><pre class="language-python">response<span class="token punctuation">.</span>reason</pre><p><br>If you simply want the status code you can use:</p><pre class="language-python">response<span class="token punctuation">.</span>status_code</pre><p><br>status_code also has an overloaded __bool__() operator so you can use an if statement and if the status_code is between 200-400, it will return TRUE.</p><br><br>------------------------------<br>Chris Porthouse<br>Senior Implementation & Integration Engineer<br>Altair<br>------------------------------<br><br>Original Message:<br>Sent: 05-10-2020 07:30 PM<br>From: Leonid Shteyngardt<br>Subject: Visual Process API error<br><br>Thank you very much Chris and Mo for your suggestions and code samples! You not only saved me time and frustration, but also provided the way to write concise and efficient code. Everything works correctly now.  I just have one more question.<br>When I try to get process status:<br><br>response = session.get(url + "/v1/visualprocesses/" + tracking_id + "/status")<br>print (response.text)<br><br>the <strong>response.text</strong> returns codes - 200, 300, 400. But when I try to run the same request in the Chrome it returns XML and descriptions: Ready, Running, Completed. Is there a way to get the same description from within the Python?<br><br>Thank you very much,<br>Leonid<br><br>------------------------------<br>Leonid Shteyngardt<br>App Dev Manager<br>Canaccord Genuity Inc.<br>New York NY<br>(212) 389-8176<br><br>Original Message:<br>Sent: 05-10-2020 10:24 AM<br>From: Chris Porthouse<br>Subject: Visual Process API error<br><br>I agree with Mo.  You need to capture the session id.  I normally create a function to handle the response:<br> <pre class="language-python"><span class="token keyword">def</span> <span class="token function">getResponse</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> jsonData<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">global</span> session <span class="token keyword">if</span> <span class="token operator">not</span> <span class="token string">'session'</span> <span class="token keyword">in</span> globals<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> session <span class="token operator">=</span> requests<span class="token punctuation">.</span>Session<span class="token punctuation">(</span><span class="token punctuation">)</span> session<span class="token punctuation">.</span>headers<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'Content-Type'</span><span class="token punctuation">:</span><span class="token string">'application/json'</span><span class="token punctuation">}</span><span class="token punctuation">)</span> r <span class="token operator">=</span> session<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> json<span class="token operator">=</span>jsonData<span class="token punctuation">)</span> <span class="token keyword">return</span> r</pre> <code>

    So when I login (with a defined function I created called 'login'):
    response = login('admin', 'password')
    I create a global "session" variable which will be used automatically when I call my getResponse function:
    def startProcess(processName):     start_url = 'http://ds13w2k12/MSAdmin/api/v1/visualprocesses/start'     print('Attempting to start process ' + processName)     return getResponse(start_url, processName)
    ------------------------------
    Chris Porthouse
    Senior Implementation & Integration Engineer
    Altair

    Original Message:
    Sent: 05-09-2020 04:55 PM
    From: Leonid Shteyngardt
    Subject: Visual Process API error

    Hello,

    I'm trying to use Monarch Visual Process Designer API from Python. Login request returns success, but visualprocess/start  returns an error. Below is a script and the output. What am I doing wrong?

    Thank you!

    monarch_rest_api.py
    import requests

    url = "http://us1-monarch1/MSAdmin/api"
    login_info = {"Username":"test_login", "Password":"test_password"}
    jobname = "Test Job"

    resp = requests.post(url + "/login", json = login_info)
    print ("Login: ", resp.status_code)

    resp = requests.post(url + "/v1/visualprocesses/start", data=jobname, headers={'Content-Type':'application/json'})
    print ("Start: ", resp.status_code)
    print (resp.json())

    Output
    H:\python>python monarch_rest_api.py
    Login: 200
    Start: 500
    {'Message': 'An error has occurred.'}

    ------------------------------
    Leonid Shteyngardt
    App Dev Manager
    Canaccord Genuity Inc.
    New York NY
    (212) 389-8176
    ------------------------------"