Solved

Python and FME Server - Unprocessable Entity error

  • 7 April 2017
  • 6 replies
  • 23 views

Userlevel 2
Badge +19

Hi.

I want to run a workspace using the Python script I have pasted here. But everytime I run it, I get the following error:

HTTP Error 422: Unprocessable Entity

My Python version is 2.7.

FME Server 2016

Thanks for any help provided!

import urllib2
import json

SERVER_URL = "http://100.100.100.100"
REPOSITORY = "MyRepo"
WORKSPACE = "First%20Test.fmw"
TOKEN = "ddddddddddddddddddddddddddddddddddddddddddddddddd"


# Set up the published parameters as object
params = {
    "publishedParameters" : [
        {
            "name" : "path_1",
            "value" : "C:\test1"
        },
        {
            "name" : "path_2",
            "value" : "C:\test2"
        }
    ]
}


url = '{0}/fmerest/v2/transformations/commands/submit/{1}/{2}'.format(SERVER_URL, REPOSITORY, WORKSPACE)

# Request constructor expects bytes, so we need to encode the string
body = json.dumps(params).encode('utf-8')

headers = {
    'Content-Type' : 'application/json',
    'Accept' : 'application/json',
    'Authorization' : 'fmetoken token={0}'.format(TOKEN)
}

# This will use POST, since we are including data
req = urllib2.Request(url, body, headers)

r = urllib2.urlopen(req)

print('Request status: ' + str(r.status))

resp = r.read()
resp = resp.decode('utf-8')
resp = json.loads(resp)

if r.status == 202:
    print('Job ID is {0}'.format(resp['id']))
icon

Best answer by david_r 11 April 2017, 10:00

View original

6 replies

Userlevel 4

At first glance I see a non-terminated string literal on line 19. Is that a copy-paste error?

Userlevel 2
Badge +19

At first glance I see a non-terminated string literal on line 19. Is that a copy-paste error?

Yes, that's a copy-paste error. I wll edit it right away,

 

 

Thanks

 

Userlevel 4

You may have to urlencode the json string becore passing it into urllib2.Request, e.g.

import urllib
...
req = urllib2.Request(url, urllib.urlencode(body), headers)

Let me know if that makes a difference.

Userlevel 2
Badge +19

You may have to urlencode the json string becore passing it into urllib2.Request, e.g.

import urllib
...
req = urllib2.Request(url, urllib.urlencode(body), headers)

Let me know if that makes a difference.

With that code I get this error:

 

 

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

 

Userlevel 4

On second thought you don't need the urlencode().

There's possibility that the backslash in your path names is causing problems. Try using forward slashes:

 

params = {
    "publishedParameters" : [
        {
            "name" : "path_1",
            "value" : "C:/test1"
        },
        {
            "name" : "path_2",
            "value" : "C:/test2"
        }
    ]

For reference, return code 422 means "The request was well-formed, but cannot be performed due to semantic errors." If you still get the error, make sure that your workspace does have two parameters "path_1" and "path_2" (attention to case).

Userlevel 2
Badge +19

On second thought you don't need the urlencode().

There's possibility that the backslash in your path names is causing problems. Try using forward slashes:

 

params = {
    "publishedParameters" : [
        {
            "name" : "path_1",
            "value" : "C:/test1"
        },
        {
            "name" : "path_2",
            "value" : "C:/test2"
        }
    ]

For reference, return code 422 means "The request was well-formed, but cannot be performed due to semantic errors." If you still get the error, make sure that your workspace does have two parameters "path_1" and "path_2" (attention to case).

Thanks to your comment I realized that path_1 is a list...

 

 

params = {
    "publishedParameters" : [
        {
            "name" : "path_1",
            "value" : ["C://test1"]
        },
        {
            "name" : "path_2",
            "value" : "C://test2"
        }
    ]
}

 

Thanks!!

 

Reply