5 Replies Latest reply on Mar 9, 2016 11:33 PM by 3189487

    How do I make Bulk API POST call using python 2?

    3189487

      I tried to make a REST API POST call by utilizing the bulk API, I tested it in Postman, it works fine:

       

       

      POST url like: https://secure.p04.eloqua.com/API/bulk/2.0/contacts/exports

      Result likes:

      {

        "name": "Contact email export",

        "fields": {

          "EmailAddress": "{{Contact.Field(C_EmailAddress)}}"

        },

        "dataRetentionDuration": "P7D",

        "uri": "/contacts/exports/30",

        "createdBy": "XXX",

        "createdAt": "2016-03-03T23:01:16.0656246Z",

        "updatedBy": "XXX",

        "updatedAt": "2016-03-03T23:01:16.0656246Z"

      }

       

       

      In my Python code:

      ....

      data = {

         "name": "Contact email export",

         "fields": {

         "EmailAddress": "{{Contact.Field(C_EmailAddress)}}"

         }

      }

       

       

      sample_exports = request.post('/contacts/exports',json.dumps(data))

      .....

      It always throw the following exception:

      urllib.py, line 1333, in urlencode   raise TypeError

      TypeError: not a valid non-string sequence or mapping object

       

       

      I think it is caused by the data format encoding issue with special character in the data, how do I get around with this?  Thanks, I am very new to Eloqua and Python.

        • 1. Re: How do I make Bulk API POST call using python 2?
          Shawn Chen-Oracle

          Building On The Eloqua Platform - A Resource Guide , search keyword Python, you will get an example. I have not try it but hope this help.

          • 2. Re: How do I make Bulk API POST call using python 2?
            Lou Patrick

            I'd recommend using the Requests library instead of urllib. If you interested in why, check out this conversation.

             

            If you share some more of the detail on how you are building the request client, in example, are you using an Eloqua request library, I could take a try at helping with this issue.

            • 3. Re: How do I make Bulk API POST call using python 2?
              Lou Patrick

              I wanted to add some more detail to my reply. If you are using Python version 2.7.10 or 2.7.11 then Requests will be the best option. If you are using a version prior to 2.7.10, then the urllib route will be the way to go.

               

              If you are using Python version 2.7.10 or 2.7.11, after you install Requests, here is a sample script using basic authentication for the call mentioned:

               

              import requests
              import json
              import base64
              
              url = 'https://secure.p04.eloqua.com/API/Bulk/2.0/contacts/exports'
              authKey = base64.b64encode('SITE_NAME' + '\\' + 'USER_NAME' + ':' + 'PASSWORD')
              headers = {"Content-Type":"application/json", "Authorization":"Basic " + authKey}
              data = json.dumps({"name": "Contact email export", "fields": {"EmailAddress": "{{Contact.Field(C_EmailAddress)}}"}})
              
              response = requests.post(url, headers=headers, data=data)
              
              parsedResponse = response.json()
              prettyResponse = json.dumps(parsedResponse, sort_keys=True, indent=4, separators=(',', ': '))
              
              print prettyResponse
              
              

               

              Hopefully this is helpful, and if you are using a Python version prior to 2.7.10 let me know.

              • 4. Re: How do I make Bulk API POST call using python 2?
                Muqadder Iqbal

                The simplest way to do this will be to utilize the "Requests" package. Installation instructions are here: Requests: HTTP for Humans — Requests 2.9.1 documentation

                 

                Once you have imported the package into your python class file (the code), calling an endpoint will be as simple as:

                 

                eloquaLoginToken = "%s\\%s:%s" %(eloquaLoginCompanyName,eloquaLoginUserName,eloquaLoginPassword)
                eloquaToken64 = base64.b64encode(eloquaLoginToken.encode('utf-8'))

                requestHeaders = {
                                  "Authorization" : "Basic " + eloquaToken64,
                                  "Accept": "application/json",
                                  "Content-Type": "application/json"
                                 }

                webResponseRaw = requests.get(endpointURL,headers = requestHeaders)

                webResponse = json.loads(webResponseRaw.text) 

                print webResponse


                Hope this helps!


                - Muqadder

                • 5. Re: How do I make Bulk API POST call using python 2?
                  3189487

                  Thanks for everyone's reply, they are all very helpful, the problem is solved already!

                  I am using the python version 2.7.11 and in the request call, I comment out the following line, and the post call works now

                  #data = urllib.urlencode(data)

                  .....

                  def request(self, method, url, data):

                    request_object = urllib2.Request(self.base_url + url)

                    request_object.get_method = lambda: method

                   

                     for key,value in self.headers.items():

                    request_object.add_header(key,value)

                   

                     # if data != None:
                    # data = urllib.urlencode(data)

                  ....