Question

I'm accessing a RESTFUL API using Powerbuilder. I have it working just fine however, I would like to call the API and include request headers. The reason I need this is because the API accepts a "Content-Type" request header, which can be set to either "application/xml" or "application/json".

Here is what I have done so far:

inet iinet_base
n_ir ir

GetContextService( "Internet", iinet_base )
CREATE n_ir
li_rc = iinet_base.GetURL( "http://api.com/apicall", ir )
ls_result = ir.of_getResultData_String()

The above will return the data as expected. The request must be a GET request, not a POST.

How do I add a Request Header to the GetURL request?

Was it helpful?

Solution

Ended up not using the inet object and instead using something else. Long story short, I'm now using an OleObject like this

lole_Send.connectToNewObject("Msxml2.DOMDocument.6.0")
lole_SrvHTTP.connectToNewObject("MSXML2.ServerXMLHTTP.6.0")
lole_SrvHTTP.Open("GET", "http://api.com/apicall", FALSE)
lole_SrvHTTP.SetRequestHeader( "Content-Type", "application/json")
lole_SrvHTTP.Send(lole_Send)
ls_message = string(lole_SrvHTTP.Status)
ls_response = string(lole_SrvHTTP.ResponseText)

There is more to it, however this is a good start for someone else trying to find an answer to this.

OTHER TIPS

You should use instead the PostUrl() method of the inet object that let you specify some request headers.

If you want to add header you can use more SetRequestHeader

Sample curl

curl -X GET --header 'Accept: application/json --header 'Authorization: asdfasdf' --header 'APIKEY: ssss'

like this

lole_Send.connectToNewObject("Msxml2.DOMDocument.6.0")
lole_SrvHTTP.connectToNewObject("MSXML2.ServerXMLHTTP.6.0") 
lole_SrvHTTP.Open("GET", "http://api.com/apicall", FALSE)

lole_SrvHTTP.SetRequestHeader( "Content-Type", "application/json")
lole_SrvHTTP.SetRequestHeader( "Authorization", "asdfasdf' )*")
lole_SrvHTTP.SetRequestHeader( "APIKEY", "ssss")

lole_SrvHTTP.Send(lole_Send)
ls_message = string(lole_SrvHTTP.Status)
ls_response = string(lole_SrvHTTP.ResponseText)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top