Android Question How to Set Headers in Okhttputils2?

sktanmoy

Active Member
Licensed User
Longtime User
I need to set Content-Type and Bearer Token with HTTP POST request. I searched the forum and failed to find any clear instruction. I tried to use **j.GetRequest.SetHeader("Content-Type","application/json")** which didn't work.

Can you guys help me, please?
 

Semen Matusovskiy

Well-Known Member
Licensed User
I use source code of OkHttp and OkHttpUtils2 - see https://www.b4x.com/android/forum/t...putils2-httputils2-source-code.82632/#content

To post/put JSON-files I added following subroutines to HttpJob.Bas
B4X:
Public Sub PostJSON (Link As String, Text As String)

    req.InitializePost2 (Link, Text.GetBytes ("UTF8"))
    req.SetContentType ("application/json")
    CallSubDelayed2 (HttpUtils2Service, "SubmitJob", Me)

End Sub

Public Sub PutJSONString (Link As String, Text As String)

    PutBytes (Link, Text.GetBytes ("UTF8"))
    req.SetContentType ("application/json")

End Sub

Request In activity (for POST):

B4X:
    Dim httpjobInstance                                                         As HttpJob

   httpjobInstance.Initialize ("", Me)
   httpjobInstance.PostJSON (<http-address>, stringJSON)
   httpjobInstance.GetRequest.SetHeader ("Authorization", "Bearer " & stringLoginAccessToken)
 
Upvote 0

Similar Threads

Top