POST request failure with code -10001

Can someone tell me where the mistake is? And what the resone?

sub init()
    print "uriHandler.brs - [init]"

    m.port = createObject("roMessagePort")
    m.top.observeField("request", m.port)
    m.top.functionName = "go"
    m.top.control = "RUN"
    m.constants = CreateObject("roSGNode", "constants")

end sub

function go() as Void
    print "uriHandler.brs - [go]"

    while true

        msg = wait(0, m.port)
        mt = type(msg)

        if mt = "roSGNodeEvent"

            if msg.getField() = "request"

                if addRequest(msg.getData()) <> true then print "Invalid request"

            else

                print "Error: unrecognized field '"; msg.getField() ; "'"

            end if

        else if mt = "roUrlEvent"

            processResponse(msg)

        else

            print "Error: unrecognized event type '"; mt ; "'"

        end if

    end while

end function

 
function addRequest(request as Object) as Boolean
    print "uriHandler.brs - [addRequest]"

    if type(request) = "roAssociativeArray"

        context = request.context

        if type(context) = "roSGNode"

            parameters = context.parameters

            if type(parameters) = "roAssociativeArray"

                if parameters.lookUp("name") <> invalid
                   
                    print "DeviceID uri"
                    uri = m.constants.uri_get_deviceID

                else if parameters.lookUp("id") <> invalid

                    print "Token uri"
                    uri = m.constants.uri_get_token

                else

                    print "Error: unrecognized parameter"

                end if

            end if

            urlXfer = CreateObject("roUrlTransfer")
            urlXfer.setUrl(uri)
            ' print uri
            urlXfer.setPort(m.port)
            urlXfer.retainBodyOnError(true)
            byteArray = CreateObject("roByteArray")
            byteArray.fromAsciiString("test@test.com:1111")
            ' print byteArray.ToBase64String()
            urlXfer.addHeader("authorization", "Basic " + byteArray.ToBase64String())
            urlXfer.addHeader("Content-Type", "application/json")
            urlXfer.addHeader("Accept", "application/json")
            requestBody = formatJson({"name":"Roku"})
            ' urlXfer.setRequest("POST")
            ok = urlXfer.asyncPostFromString(requestBody)
            print ok

        end if

    end if

    return true

end function

sub processResponse(msg as Object)
    print "uriHandler.brs - [processResponse]"

    code = msg.getResponseCode()
    reason = msg.getFailureReason()

    print code
    print reason

end sub

Output to the console:

login.brs - [init]
uriHandler.brs - [init]
uriHandler.brs - [go]
login.brs - [buttonPressed]
login.brs - [onKeyboardOKPressed]
login.brs - [buttonPressed]
login.brs - [onKeyboardOKPressed]
login.brs - [onConfirmPressed]
login.brs - [loginClient]
login.brs - [makeRequest]
uriHandler.brs - [addRequest]
DeviceID uri
true
uriHandler.brs - [processResponse]
-10001
Cancelled

Hello I believe you might be missing one line where you have to set certificates and this is a built in roku function


urlTransfer = CreateObject("roUrlTransfer")

urlTransfer.SetCertificatesFile("common:/certs/ca-bundle.crt")

urlTransfer.SetUrl(url)

urlTransfer.AsyncGetToString()


request = createObject("roUrlTransfer")

request.setCertificatesFile("common:/certs/ca-bundle.crt")

request.setUrl(url) json = request.getToString()

response = parseJSON(json)

There may be some stuff missing still but both of those samples above work when headers are not involved when headers are involved before you send the request you can add in


request.headers["Authorization"] = .... etc

Hope that helps :slightly_smiling_face:

Also I would like to invite you to the very supportive Roku Developers Slack community where you can meet like minded Roku developers and get quick answers on future problems

https://join.slack.com/t/rokudevelopers/shared_invite/zt-22n9u11c1-zoqCczrSTRiMxid4kWBPRA

just post in the General channel to get started :slightly_smiling_face:

Thank you for your response. My changes:

function addRequest(request as Object) as Boolean 
    print "uriHandler.brs - [addRequest]"

    if type(request) = "roAssociativeArray"

        context = request.context

        if type(context) = "roSGNode"

            parameters = context.parameters

            if type(parameters) = "roAssociativeArray"

                if parameters.lookUp("name") <> invalid
                    
                    print "DeviceID uri"
                    uri = m.constants.uri_get_deviceID

                else if parameters.lookUp("id") <> invalid

                    print "Token uri"
                    uri = m.constants.uri_get_token

                else 

                    print "Error: unrecognized parameter"

                end if

            end if

            urlXfer = CreateObject("roUrlTransfer")
            urlXfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
            urlXfer.InitClientCertificates()
            urlXfer.setUrl(uri)
            ' print uri
            urlXfer.setPort(m.port)
            urlXfer.retainBodyOnError(true)
            byteArray = CreateObject("roByteArray")
            byteArray.fromAsciiString("test@test.com:1111")
            ' print byteArray.ToBase64String()
            urlXfer.addHeader("authorization", "Basic " + byteArray.ToBase64String())
            urlXfer.addHeader("Content-Type", "application/json")
            urlXfer.addHeader("Accept", "application/json")
            requestBody = formatJson({"name":"Roku"})
            ' urlXfer.setRequest("POST")
            ok = urlXfer.asyncPostFromString(requestBody)
            print ok

        end if

    end if

    return true

end function

But it still doesn’t work. I didn’t quite understand about these certificates. If it doesn’t work yet, it means that the sertificate is not suitable?

I think the certificates line is a built-in Roku brightscript line so there is nothing to go wrong with it…

Hello are you getting a response if you switch to this fake url

https://jsonplaceholder.typicode.com/guide/

That is javascript code but you should still receive a response if you

a) switch the url you are using to the fake POST url

b) change the body of the item you are POSTing

c) just log out the response

Thanks, @philanderson777 . I found a solution on slack. I’ll post a solution later.

Glad to hear it @korroziea