Can someone help me understand how to make a REST POST request in HPCC? I've read through the documentation but I couldn't find any examples. Any help would be appreciated.
2 Answers
Here is an example of an HTTP POST that sends JSON content. It was a working example, but unfortunately the example service it calls no longer seems to be available.
The fact that is being provided to send, and the format specified is JSON it is what causes it to do a POST with JSON content.
Notice the format of the call matches closely with how SOAPCALL works but in this case will send and receive content as JSON. There is a JIRA open to get HTTPCALL POST added to the documentation, but in the meantime you can use the SOAPCALL documentation as a guideline for what additional options might be available. Most of the options you can add to a SOAPCALL can also be used with an HTTPCALL POST.
Also note that the "service name" is passed in as ''. Filling in the service name automatically adds another layer of JSON around the record that creates a JSON object named after that parameter. That's not usually what you want.
sendContent := RECORD
string name {XPATH('name')} := 'bob';
string salary {XPATH('salary')} := '22';
string age {XPATH('age')} := '105';
END;
receiveContent := RECORD
string name {XPATH('name')};
string salary {XPATH('salary')};
string age {XPATH('age')};
integer4 id {XPATH('id')};
END;
receiveRec := RECORD
string status {XPATH('status')};
receiveContent content {XPATH('data')};
END;
OUTPUT(HTTPCALL('https://dummy.restapiexample.com/api/v1/create', '', sendContent, DATASET(receiveRec), JSON, LOG));

- 56
- 2
HTTPCALL is a function that calls a REST service. Here is an example from the Language Reference Manual:
worldBankSource := RECORD
STRING name {XPATH('name')}
END;
OutRec1 := RECORD
DATASET(worldBankSource) Fred{XPATH('/source')};
END;
raw := HTTPCALL('http://api.worldbank.org/sources', 'GET', 'text/xml', OutRec1, );
OUTPUT(raw);
////Using HTTPHEADER to pass Authorization info
raw2 := HTTPCALL('http://api.worldbank.org/sources', 'GET', 'text/xml',
OutRec1, HTTPHEADER('Authorization','Basic
dXNlcm5hbWU6cGFzc3dvcmQ='),HTTPHEADER('MyLiteral','FOO'));
OUTPUT(raw2);
Hope this helps!
Bob

- 164
- 7