· Feb 14, 2019

REST API Post Header and Body


I'm trying to send a request to a REST API Service.

I'm using this code (Adapter is  "EnsLib.HTTP.OutboundAdapter")

Method Put(pRequest As SIGE.Grupo.BO.Operation.putRequest, Output pResponse As SIGE.Grupo.BO.Operation.putResponse) As %Status
set tSC = $$$OK


     set tFormVar="Content-Type,apikey"
     set tContent="application/json"
     set tApiKey="somekey"

     Set tOut=##class(%IO.StringStream).%New()
     set tSC= ..ObjectToJSONStream(pRequest,.tOut)

     Set tSC=..Adapter.Put(.tHttpResponse,tFormVar,tContent,tApiKey,tOut.Data)
If $$$ISERR(tSC)&&$IsObject(tHttpResponse)&&$IsObject(tHttpResponse.Data)&&tHttpResponse.Data.Size {
Set tSC=$$$ERROR($$$EnsErrGeneral,$$$StatusDisplayString(tSC)_":"_tHttpResponse.Data.Read())

    Set tSC=$$$SystemError
    quit tSC

but I got a missing message alert from the REST Service

Also, I tried with this  set tFormVar="Content-Type,apikey,cur" but I got a duplicate apikey message.

This is the right way to send a header and body to a PUT operation in REST API?

Thanks & Best Regards


Discussion (1)1
Log in or sign up to continue

I'm not that familiar with adapters, but the documentation suggests that you need to use the SkipBodyAttrs property to send a header like Content-Type. Also, I think you need to decide whether you're sending form data or a body. When tFormVar is "Content-Type,apikey", the documentation says that your third data argument will be assigned to the last form variable, apikey, which is almost certainly not what you want. Your second try with three variables looks more likely to work, depending on what the service expects in the body.

I don't know anything about the duplicate apikey. That's presumably specific to the service you're calling.