Search code examples
apama

Error Parsing HTTP Response Using Apama HTTP Client plugin


I am trying to do an HTTP call to a REST API using Apama HTTP Client plugin. I am able to send a request to the REST Resource, but while parsing the response, I am getting the below error.

WARN  [20176] - Failed to parse the event "`{contentType:application/json,sag.type:apamax.httpserversample.HTTPResponse,http:{headers:{contentLength:50,content-type:application/json,content-length:50},statusCode:200,method:POST,path:/rest/POC_422837/WS/provider/apamaTestConn,cookies:{},statusReason:OK}}{body:{status:Hello Apama. How are you doing?}}" 
   from httpClient due to the error: Unable to parse event apamax.httpserversample.HTTPResponse: 
   Unable to parse string from the map '{status:Hello Apama. How are you doing?}': 
   Invalid datatype, could not cast to string` 

The YAML Config file looks as below,

connectivityPlugins:
  HTTPClientGenericTransport:
    libraryName: connectivity-http-client
    class: HTTPClient

startChains:
  httpClient:
    - apama.eventMap
    - mapperCodec:
        apamax.httpserversample.HTTPRequest:
          towardsTransport:
            mapFrom:
              - metadata.http.path: payload.path
              - metadata.requestId: payload.id
              - metadata.http.method: payload.method
              - payload: payload.data
            defaultValue:
              - metadata.contentType: application/json
              - metadata.sag.type: HelloWorld
        apamax.httpserversample.HTTPResponse:
          towardsHost:
            mapFrom:
              - payload.body: payload
              - payload.id: metadata.requestId
        apamax.httpserversample.HTTPError:
          towardsHost:
            mapFrom:
              - payload.id: metadata.requestId
              - payload.code: metadata.http.statusCode
              - payload.message: metadata.http.statusReason
    - classifierCodec:
        rules:
          - apamax.httpserversample.HTTPResponse:
            - metadata.http.statusCode: 200
          - apamax.httpserversample.HTTPError:
            - metadata.http.statusCode:
    - jsonCodec:
        filterOnContentType: true
    - stringCodec
    - HTTPClientGenericTransport:
        host: ${CORRELATOR_HOST}
        port: ${CORRELATOR_PORT}

Please help.


Solution

  • I believe the problem is that you map in the config

        apamax.httpserversample.HTTPResponse:
          towardsHost:
            mapFrom:
              - payload.body: payload
              - payload.id: metadata.requestId
    

    the payload of the response to HTTPResponse.body.

    However, as you can see from the warning, the payload is actually a map, so you need to do

    - payload.body: payload.status