2

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?

3 Answers 3

1

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.

0

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

1
  • I forgot to mention that the API only accepts GET requests, not POST requests. I've edited my question to reflect this.
    – Lydon
    Oct 10, 2013 at 6:25
0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.