NetTalk Central

Author Topic: How to set the HTTP headers  (Read 6834 times)

Rene Simons

  • Hero Member
  • *****
  • Posts: 650
    • View Profile
How to set the HTTP headers
« on: January 26, 2020, 08:54:19 AM »
Hi all,

I have acces to an API where I can obtain inof I need for my web-application.
To use it I need to set the HTTP-headers. The specs for the header can be seen in the attached image.
I have no idea on how to set these headers.

I guess I can call the URL after I set the HTTP-headers.
And then retrieve the received result in the Pagereceived embed.

Thanks in advance for helping me out here.

René

Rene Simons
NT14.14

Jane

  • Sr. Member
  • ****
  • Posts: 372
  • Expert on nothing with opinions on everything.
    • View Profile
    • Email
Re: How to set the HTTP headers
« Reply #1 on: January 26, 2020, 01:44:13 PM »
I've found it useful to experiment with the web client in the basic NetTalk demo program to get bits of communication going with an external API, then copying that code into my own app.

FWIW, here's some code I use for API calls to one service:
Quote
    ThisWebClient.SetAllHeadersDefault()

    ThisWebClient.HeaderOnly = 0
    ThisWebClient.Cookie = ''
    ThisWebClient.CustomHeader = 'Authorization: Bearer '&clip(pToken)
    ThisWebClient.Referer = ''   
    ThisWebClient.AsyncOpenTimeOut = 2200       ! 22 seconds
    ThisWebClient.InActiveTimeout = GLO:QueryTimeout            ! 6000             
    ThisWebClient.SSLMethod = NET:SSLMethodTLS
    ThisWebClient.HTTPVersion = 'HTTP/1.1'
    ThisWebClient.ContentType = 'application/x-www-form-urlencoded'
    ThisWebClient.ConnectionKeepAlive = FALSE
    ThisWebClient.CanUseProxy = GLO:CanUseProxy
    ThisWebClient.Fetch(clip(pURL))

and this is the POST to the same service to get its bearer oauth token:

Quote
        ThisWebClient.SetAllHeadersDefault()

        ThisWebClient.HeaderOnly = 0
        ThisWebClient.Cookie = ''
        ThisWebClient.CustomHeader = ''
        ThisWebClient.Referer = ''
        ThisWebClient.Authorization = ThisWebClient.CreateAuthorizationString (LOC:Key, LOC:Secret, Net:WebBasicAuthentication,_AuthNoWrap) 
        ThisWebClient.AsyncOpenTimeOut = 1200       ! 12 seconds
        ThisWebClient.InActiveTimeout = 2000        ! 20 seconds     
        ThisWebClient.SSLMethod = NET:SSLMethodTLS
        ThisWebClient.HTTPVersion = 'HTTP/1.1'
        ThisWebClient.ContentType = 'application/x-www-form-urlencoded'
        ThisWebClient.ConnectionKeepAlive = FALSE
        ThisWebClient.CanUseProxy = GLO:CanUseProxy

     
    ThisWebClient.Post(LOC:URL,clip(LOC:PostString)) 

Rene Simons

  • Hero Member
  • *****
  • Posts: 650
    • View Profile
Re: How to set the HTTP headers
« Reply #2 on: January 27, 2020, 09:45:23 AM »
Thank you Jane.
That actually helped a lot.
Rene Simons
NT14.14

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: How to set the HTTP headers
« Reply #3 on: January 29, 2020, 04:44:22 AM »
Hi Rene,

shout if you need more. There are various properties you can set (which turn into headers) and also a CustomHeader property you can set as well.

cheers
Bruce

Rene Simons

  • Hero Member
  • *****
  • Posts: 650
    • View Profile
Re: How to set the HTTP headers
« Reply #4 on: January 30, 2020, 03:35:05 AM »
Bruce,

With the tips Jane gave, I managed to to use a third party api and retrieve the json data I need.
Structurewise, the json varies from time to time so I have to figure out how I am going to retrieve the data.

But since you are asking :-) ...
I do a ThisWebClient.fetch to get the data.
Is there any simple or not so hard way I can put the result in a StringTheory object so I can manipulate things?

Cheers,
René
Rene Simons
NT14.14

Jane

  • Sr. Member
  • ****
  • Posts: 372
  • Expert on nothing with opinions on everything.
    • View Profile
    • Email
Re: How to set the HTTP headers
« Reply #5 on: January 30, 2020, 08:42:15 AM »
I've only done one significant API project, Rene, so my experience is limited.  But this one has been working fine for a year and a half and FWIW, this is the approach I took.

Other than getting the bearer token, my other interactions involve a ThisWebClient.Fetch method.

I've packaged that method in a separate Window procedure (SendGetPacket), and each of the data items I'm calling uses that procedure.

The prototype for SendGetPacket is (STRING pURL, STRING pToken,StringTheory pST),LONG

The URL is built by the calling procedure to have exactly what is needed for this particular fetch.  The token is...  um... the bearer token.  And the StringTheory object is what will receive the data in the calling procedure.

On EVENT:OpenWindow, SendGetPacket runs the Fetch code I pasted in my earlier message.

There's the ErrorTrap code copied directly from Bruce's example.

There's no doubt a better way to do the next bit, but I wrote something that worked and haven't bothered trying to improve that.  SendGetPacket has its own StringTheory object (ST1) which I just use to make sure there's a '200' success response in what I've received.  LOC:RV is the LONG for the prototype's return.

In the ThisWebClient,PageReceived, after the parent, I have this code to remove the header and to put the body of what was received into the StringTheory object passed by the calling procedure:

Quote
            st1.SetValue(ThisWebClient.ThisPage.GetValue())
            ST1.split('<13>')           
                LOC:ResultLine = ST1.GetLine(1)
            if Instring('200',LOC:ResultLine,1,1)
                LOC:RV = TRUE
                self.RemoveHeader()
                pST.SetValue(ThisWebClient.ThisPage.GetValue())
            ELSE
                LOC:RV = FALSE
                if Instring('ERROR',upper(LOC:ResultLine),1,1)
                    AddErrorLog(LOC:ResultLine)
                    loop ERR# = 1 to ST1.Records()
                        if instring('detailedm',ST1.GetLine(ERR#) ,1,1)
                            AddErrorLog(ST1.GetLine(ERR#))
                            BREAK
                        end ! if
                    end ! loop                   
                end ! if               
            end ! if   
            post(event:closewindow)

The procedures calling this are also Window procedures, and I have one of them customized for each data item I'm receiving (doctors, patient appointments, service locations, etc.)
The "Web" object below is a simple class I wrote derived from StringTheory to built the parameter string URLs for the API calls.

As you can see, I basically prepare the URL, call my ClientSendPacket and pass it that URL and my calling StringTheory object. 
Then load the contents of the StringTheory object into a jfiles object and do whatever processing after that.

Quote
        ST.start() 
        web.ClearParms()
        web.SetBaseURL(clip(GLO:PracticeID)&'/departments' )
        web.AddParm('showalldepartments='&choose(pShowAllDepts,'true','false')  )
        web.AddParm('providerlist='&choose(pProviderList,'true','false') )       
       

        ClientSendPacket(web.GetURL(),ST)
   
        JsonData.Start 
        JsonData.SetTagCase(jf:CaseAny)
        JSONData.Load(ST)

HTH

Cheers,

Jane

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: How to set the HTTP headers
« Reply #6 on: January 30, 2020, 09:45:02 PM »
Hi Rene,

>> I do a ThisWebClient.fetch to get the data.
>> Is there any simple or not so hard way I can put the result in a StringTheory object so I can manipulate things?

To summarize Jane's post somewhat - it's already in a StringTheory object.
The result returns in the PageReceived method, in self.thispage - which is a StringTheory object.

cheers
Bruce


Rene Simons

  • Hero Member
  • *****
  • Posts: 650
    • View Profile
Re: How to set the HTTP headers
« Reply #7 on: January 31, 2020, 03:26:22 AM »
Bruce,

Thanks again.
Jane's additional information made things a lot clearer and also gave me some things to think about.
Thanks to Jane as well.

Cheers,
Rene
Rene Simons
NT14.14