Announcement

Collapse
No announcement yet.

How to pass an array via REST as a parameter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass an array via REST as a parameter

    I am trying to pass an array using Python to a custom controller action. I'm not able to get an array out of the request object regardless of what I try. The last array item seems to be accessible, but nothing before it. In the example below, I'm expecting to get the following array in PHP: ['a', 'b', 'c', 'd', 'e']

    Here is the python request:

    Code:
       def getJobData(self, jobNumber, exclude, dateStart, dateEnd):
         params = {
           "jobNumber": jobNumber,
           "exclude":   ['a', 'b', 'c', 'd', 'e'],
           "dateStart": dateStart,
           "dateEnd":   dateEnd
         }  
         endpoint = "Invoice/action/CollectJobData"
    
         logging.debug(f"Collectiong job data ({endpoint}, {params})")
    
         self.makeRequest(endpoint, params)
    which produces this in the Python log:

    Code:
    Making a request (Invoice/action/CollectJobData, {'jobNumber': '18-0089', 'exclude': ['a', 'b', 'c', 'd', 'e'], 'dateStart': '2000-01-01', 'dateEnd': '2020-02-05'}

    This is the code in the controller:

    PHP Code:
    public function getActionCollectJobData($params$inpData$request) {
        
    $jobNumber  $request->get('jobNumber');
        
    $exclude    $request->get('exclude');
        
    $dateStart  $request->get('dateStart');
        
    $dateEnd    $request->get('dateEnd');

        
    $GLOBALS['log']->error("params",[$jobNumber$exclude$dateStart$dateEnd],[]);

    which produces this in the EspoCRM log:

    Code:
    [2020-02-06 01:10:23] Espo.ERROR: params ["18-0089","e","2000-01-01","2020-02-05"] []
    I don't know if I'm doing something wrong in the code in Python or PHP. How is the EspoCRM API setup to pass array parameters into controllers? I can't find an example anywhere.

  • #2
    I never figured out how to do this with a GET request, but here is a solution with a POST request.

    This is the Python code to submit a request using the requests module:
    Code:
    import requests
    import base64
    
    baseUrl = "https://espo.domain.com/api/v1/"
    username = "..."
    password = "..."
    token = base64.b64encode(f"{username}:{password}".encode()).decode()
    
    
    def makeRequest(method, endpoint, params, json = None):
      headers = {
        "Content-Type": "application/json",
        "Espo-Authorization": token
      }  
      if method == "get":
        r = requests.get (baseUrl + endpoint, headers = headers, params = params)
      else:
        r = requests.post(baseUrl + endpoint, headers = headers, params = params, json = json)
      return r
    
    jsonObj = {
      'stuff': [1,2,3],
      'moreStuff': 'a string'
    }
    
    qParams = {
      'foor: 'bar'
    }
    
    makeRequest(method, endpoint = "Invoice/action/ExampleEndpoint, params = qParams, json = jsonObj)

    This is the PHP code in custom/Espo/Custom/Controllers/ExampleEntity.php:

    PHP Code:
    <?php

    namespace Espo\Custom\Controllers;

    use 
    Espo\Core\Exceptions\BadRequest;

    class 
    ExampleEntity extends \Espo\Core\Templates\Controllers\Base
    {
      public function 
    postActionExampleEndpoint($params$inpData$request) {
        
    # Store the query parameters, e.g. {url}?foo=bar
        
    $foo $request->get('foor'); # Stores the string 'bar'

        # If applicable, check to be sure $inpData is an object (a dictionary object in Python)
        
    if(gettype($inpData) != "object")
          throw new 
    BadRequest("Invalid input data: unexpected type");

        
    # Store or use the dictionary/JSON/object data
        
    $stuff $inpData->stuff # Stores the array (1,2,3)
        
    $moreStuff $inpData->moreStuff # Stores the string 'a string'

        # See parameters and object
        
    $GLOBALS['log']->error("params",[$key$inpData],[]);

        
    # Return all input parameters and objects as an associate array (dictionary) to Python
        
    return array(
          
    'stuff' => $stuff,
          
    'moreStuff' => $moreStuff,
          
    'foo' => $foo
        
    );
      }
    }

    Also, there are guides to writing and using API clients in the EspoCRM documentation, which may be helpful:
    Last edited by bandtank; 02-07-2020, 04:29 PM.

    Comment


    • #3
      Thanks for posting the solution in full bandtank , I am currently working on developing an SMS plugin that would allow EspoCRM to send SMS messages using an inexpensive Raspberry - GSM Modem - SIM card combination without having to go through a gateway service and your code example helps a lot.

      ​​​​​​I will post any progress on this project.

      Comment

      Working...
      X