Sample code for 30+ languages & platforms
PureBasic

Get the JWS Payload into a StringBuilder

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetPayloadSb, which writes the payload of a loaded JWS as text into a StringBuilder, decoded using the given charset.

Background. In practice, validate the signature before trusting the payload.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJws.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    success.i = 0

    jws.i = CkJws::ckCreate()
    If jws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Load the JWS compact serialization.
    jwsCompact.s = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
    success = CkJws::ckLoadJws(jws,jwsCompact)
    If success = 0
        Debug CkJws::ckLastErrorText(jws)
        CkJws::ckDispose(jws)
        ProcedureReturn
    EndIf

    ;  Get the JWS payload as text into a StringBuilder, decoded using the given charset.
    sbPayload.i = CkStringBuilder::ckCreate()
    If sbPayload.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJws::ckGetPayloadSb(jws,"utf-8",sbPayload)
    If success = 0
        Debug CkJws::ckLastErrorText(jws)
        CkJws::ckDispose(jws)
        CkStringBuilder::ckDispose(sbPayload)
        ProcedureReturn
    EndIf

    Debug CkStringBuilder::ckGetAsString(sbPayload)


    CkJws::ckDispose(jws)
    CkStringBuilder::ckDispose(sbPayload)


    ProcedureReturn
EndProcedure