Send POST to REST server

Dear all,

What would be the most appropriate component to send a POST request to a REST server and get JSON formated results?

Using curl (outside PP) I can write the folowing command, which returns JSON.

curl -X POST -H 'Accept:application/json' -H 'Content-Type:application/json' -d 'my query to the server' -v http://server:port/rest/destination

The idea would be to send the command from PP, get a single record back containing JSON formated results and parse it using the Data from JSON component.

Thanks,

Pierre

Answers 7
HP
HP 2014-01-19

Hi Pierre,

The HTTP Connector is the best component for this task.

Kind regards,

Jason Benedict

PD
PD 2014-01-19

Hi Jason, thanks for the quick answer.

I'm able to send a GET request with this protocol but I'm not sure how to setup a POST request. Header setup is ok, in which parameter would you put the query (-d parameter in curl), whci has to be JSON formated for the remote server?

Kind regards,

Pierre

GM
GM 2014-01-22

I've had sucess using the Request Body parameter of HTTP Connector for POST requests.  Enter your JSON string surrounded by single quotes.

'{"person":{"name":"bob","age":25}}'

For multi-line JSON each line needs to be enclosed in single quotes and concatenated using Pilotscript (.) dot operator.

'{' .

  '"person" : {' .

    '"name" : "bob",' .

    '"age" : 25' .

  '}' .

'}';

PD
PD 2014-01-22

I did put the exact same query (single line b/ single quptes) as in the curl call above in the "request body" parameter of the HTTP Connector component, it still returns "bad request". Is this the right parameter?

HTTP request to server failed. 400 - Bad Request

CHttp::CheckResponseHeader: Pipeline Pilot exception rethrown

CHttp::internalOpen: Pipeline Pilot exception rethrown

CHttp::OpenEx: Pipeline Pilot exception rethrown

CHttpPost::onProcess: Pipeline Pilot exception rethrown

-> HTTP Connector  (HTTP Connector) - error during Data Record Processing phase

CProtocolStd::onProcess: Pipeline Pilot exception rethrown

CProtocol::onProcess: Pipeline Pilot exception rethrown

CComponentRunner::CallOnProcess: exception caught, calling onFinalize before rethrow

GM
GM 2014-01-23

To further investigate the http 400 error try entering 400 in the 'Additional Allowed Response Codes' parameter of HTTP Connector.

This should avoid the PP error dialog and send full server response out the pass port.  FWIW I'm using PP8.5

PD
PD 2014-01-23

Hi Mike, actually I was using PP8.5, we have not yet migrated our servers, that should be done soon. I just tried with PP9.1, the component parameters are better organised with the "Send request body" and "Content type" parameters.

It's now working well with 9.1. I'll figure out what was wrong with 8.5, I tried many different things, so I must have missed the correct combination!

Hopefully, parsing json results might be easier. Get rather than Post return error 405, so the issue was elsewhere.

Thanks

MP
MP 2014-01-23

Maybe you are not specifying the POST method in the parameters? Incidentally, which version of PP are you using, since the HTTP Connector had something of a face lift in the 9.x releases.

In 9.x:

  • Set the Source parameter to your target endpoint.
  • Set Http Method to Post - type it in.
  • Open up the Http Method group.

  • OPTION 1: If you need to send a JSON-formatted POST body:
    • Set Http Body Content to Send Request Body.
    • Set the Request Body expression to your JSON encased in apostrophes since you want the literal value -

               e.g.   '[{"key1":"value1"},{"key2":"value2"}]'

    • Set the Content Type to application/json (this is a string literal, so no quotes).

  • OPTION 2: If you need to send a POST parameter with a JSON-formatted value:
    • Set Http Body Content to FormURLEncoded
    • Open the Parameters group.
    • Set Parameter1/Name to the required parameter name.
    • Set Parameter1/Value to the JSON expression value: eg '[{"key1":"value1"},{"key2":"value2"}]'

  • Open up the Additional Request headers group.
  • Set Header 1/Name to Accept and the Value to 'application/json' - this one is an expression.


Hope that helps...

Mike