Question
· Jul 31, 2017

Posting to a REST service

Hello,

 

I come from a programming background, but haven't touched ObjectScript before my current job, so please forgive my ignorance.

 

Within an Ensemble system, I need to post some information to a RESTful web service. I'm just trying to figure out how to create a method which takes a simple message and posts this off to a RESTful web service?

 

Thanks in advance for any help.

Discussion (3)0
Log in or sign up to continue

If it's a part of Ensemble Production, you need to create Business Operation. Here's a sample BO that does POST request:

/// This operation does a POST request to a REST API and receives Auth token
Class Production.Operation.NLPAuthOperation Extends Ens.BusinessOperation
{

Parameter ADAPTER = "EnsLib.HTTP.OutboundAdapter";

Property Adapter As EnsLib.HTTP.OutboundAdapter;

Parameter INVOCATION = "Queue";

/// Get Auth token
Method GetAuth(request As Ens.Request, Output response As Ens.StringResponse) As %Status
{
    #dim sc As %Statis = $$$OK
    
    // Form request body (using Credentials)
    set input = {"user": ( ..Adapter.%CredentialsObj.Username), "pass": (..Adapter.%CredentialsObj.Password)}
    
    // Send post request
    set sc = ..Adapter.Post(.httpResponse,,input.%ToJSON())
    quit:$$$ISERR(sc) sc
    
    // Get token from response
    set token = {}.%FromJSON(httpResponse.Data).token

    //
    set response = ##class(Ens.StringResponse).%New(token)
    quit sc
}

XData MessageMap
{
<MapItems>
    <MapItem MessageType="Ens.Request">
        <Method>GetAuth</Method>
    </MapItem>
</MapItems>
}

}

If you're outside of Ensemble, you need to use %Net.HttpRequest class. Here's an example.

Hi there,

Apologies for my late response - I had a bit of a family emergency for a while there...

Thank you for the help. I swear I'm almost there but must just be missing something. Specifically, I'm posting to ElasticSearch, and it seems to be going well, solving connectivity issues and getting past a 404 response. But I'm now getting a 400 (Bad Request) response, and I can't easily get past it... Is that something I should post more details on here, or is it worth asking a new question about that?

That may mean one of the several things:

  • You're not specifying or specifyiong incorrectly one or several request header parameters. One of the most often encountered errors is not specifying ContentType as application/json.
  • Request content is formed incorrectly

Anyway, to solve this problem you need to make a correct request to the target system, record it and compare to the Ensemble request. Here's an article on that.