Forum Discussion

spradhan7656's avatar
spradhan7656
Channel Surfer
2 months ago

i am try calling a getApi but still response code 403 ?

sub Init()
    m.top.functionName = "GetContent"
end sub
sub GetContent()

    m.deviceRegistry = CreateObject("roRegistry")
    m.storeSection = CreateObject("roRegistrySection", "Store")

    rootChildren = []
    rows = {}

    data = MakeGetRequest("https://natyf.com/api/home")
    if data = invalid
        print " Error: Failed to fetch data"
        return
    end if
    rows.title="category"
    rows.children.push(data)
    rootChildren.push(rows)

    contentNode = CreateObject("roSGNode", "ContentNode")
    contentNode.Update({
        children: rootChildren
    }, true)
    m.top.content = contentNode

end sub

function MakeGetRequest(src as string) as object
    headers = {
        "Content-Type": "application/json"
        "api_token": "j8mRCY1X9lPSjF8gcmcRNn1SO8S6MCpAReYRoAqJc9CFmPHZFVNoqJgFYQNk"
    }
    request = CreateObject("roUrlTransfer")
    port = CreateObject("roMessagePort")
    request.setMessagePort(port)
    request.SetCertificatesFile("common:/certs/ca-bundle.crt")
    request.InitClientCertificates()
    request.SetHeaders(headers)
    request.SetUrl(src)

    if request.AsyncGetToString()
        msg = wait(5000, port) ' 5-second timeout
        if msg = invalid
            print " Request timed out"
            request.AsyncCancel()
            return invalid
        end if
       
        if type(msg) = "roUrlEvent"
            code = msg.GetResponseCode()
            responseBody = msg.GetString()
            print "Response Code: "; code
            print "Response Body: "; responseBody
            print "Headers: "; headers
            print "URL: "; src
            print "Response Code: "; msg.GetResponseCode()
            print "Response Body: "; msg.GetString()
           
            if code = 200
                response = ParseJson(responseBody)
                if response = invalid
                    print "⚠️ Failed to parse JSON"
                    return invalid
                end if
                return response
            else
                print "⚠️ HTTP Error: "; code
                return invalid
            end if
        end if
    end if
   
    return invalid
end function


1 Reply

  • renojim's avatar
    renojim
    Community Streaming Expert

    Just a guess since I have no idea what natyf.com is, but looking at the error in a browser you need an Api_token header.