Subscribe
Accepted Solution

WFA - Enabling JSON Responses

I'm looking to get a WFA instance to respond via JSON when making REST-based calls to it. Right now when I query "https://server-name/rest/workflows" or any other REST-based URLs, I get back an XML response. The WFA REST primer shows that it supports JSON, but I'm unsure how to get it to talk this way. Do I need to configure or enable something? Appreciate the help.

Frequent Contributor

Re: WFA - Enabling JSON Responses

Hi,

 

You need to specify the Content-Type in the http header as - Content-Type: application/json.

If you are invoking REST APIs from a script or an application, you can easily set the header but i don't think there is a way to do it from a broswer.

Re: WFA - Enabling JSON Responses

Hey. First off, thanks for the quick response. I've already tried setting the Content-Type to application/json and get a "500 cannot consume content-type" status code. Similarly, if I set the Accept header to application/json, I get a "500 no match for accept header". I'm thinking I need to enable something server-side, or that perhaps WFA wasn't installed properly. Thanks

Re: WFA - Enabling JSON Responses

Which WFA version are you using ?  If possible, please add a code snippet of your REST API call using JSON

Re: WFA - Enabling JSON Responses

What language are you using to write your code? Can help if it is either PowerShell or Python.

 

Yaron

Re: WFA - Enabling JSON Responses

I'm using Python, in fact. Here's the snippet of code:

 

connection = httplib.HTTPSConnection(WFAServer)
connection.request('GET', '/rest/workflows?name=' + workflowName, headers = {"Authorization" : "Basic %s" % encodedCredentials, "Accept" : "application/json"})

 

It works when Accept is application/xml, but I'd prefer to use JSON. Thanks

Re: WFA - Enabling JSON Responses

Here is a code snippet in Python getting all available workflows using json. Hope this helps.

 

import requests
from requests.auth import HTTPBasicAuth
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
import json

#============================================================================
# CHANGE IF NEEDED PRIOR TO IMPORTING
url = "https://wfa.demo.netapp.com/rest/workflows"
#============================================================================

#============================================================================
# THE CODE BELOW IS NEEDED TO AVOID SSL3 HANDSHAKE ERRORS WHEN RUNNING REST
# API AGAINST NETAPP ONCOMMAND WORKFLOW AUTOMATION SERVER
CIPHERS = (
    'ECDH+AESGCMSmiley Very HappyH+AESGCM:ECDH+AES256Smiley Very HappyH+AES256:ECDH+AES128Smiley Very HappyH+AES:ECDH+HIGH:'
    'DH+HIGH:ECDH+3DESSmiley Very HappyH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
    '!eNULL:!MD5Smiley Very HappyES-CBC3-SHA'
)
class DESAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False,*args, **kwargs):
        context = create_urllib3_context(ciphers=CIPHERS)
        kwargs['ssl_context'] = context
        self.poolmanager = PoolManager(
            num_pools=connections, maxsize=maxsize,
            block=block, ssl_version=ssl.PROTOCOL_SSLv3,*args, **kwargs)
#============================================================================


# Function GetListOfWorkflows queries WFA server for available APIs (workflows) based on user's role
def GetListOfWorkflows(username,password):
    requests.packages.urllib3.disable_warnings()   
    s = requests.Session()
    s.mount(url, DESAdapter())
    head = {"Content-type": "application/json","Accept":"application/json"}
    ret=s.get(url,headers=head,auth=HTTPBasicAuth(username,password),verify=False)
    if (ret.status_code != 200):
        print ("Could not connect to API server.")
    else:
        d = []
        for wf in ret.json():
            try:
                # Check if workflow has description defined
                tmp = wf['description']
                d.append({
                    'name':wf['name'],
                    'description':wf['description']
                })
            except:
                d.append({
                    'name':wf['name']
                })
        print (json.dumps(d,indent=4))

Re: WFA - Enabling JSON Responses

Not sure why the smiley faces are there. Change the smiley face with : and D without the spaces (or the word and). Essentially the two charactes colon and upper case D.

 

Yaron

Re: WFA - Enabling JSON Responses

@lil_baby_james

 

You need to add the following key-value pair in your GET Request header.

 

 'Accept'='application/json'

 

 

for POST/PUT you need to additionally provide Content-Type='Application/json'

 

You language is python so I suggest you use the Apache requests lib. Its the best one for calling  http web requests.

 

EXample:

 

import requests
url = 'http://localhost/rest/system'
session = requests.Session()
session.auth = ('sinhaa', 'Netapp')
session.headers.update({'content-type': 'application/json'})
session.headers.update({'Accept': 'application/json'})

resp = session.get(url)
print "STATUS : %s \n" % resp.status_code
print "CONTENT : %s \n" % resp.content
If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

Re: WFA - Enabling JSON Responses

I've tried setting the Content-Type and Accept headers to application/json and the server keeps responding with a 500 code. It only works with these headers set to application/xml.