Android Question Http OPTIONS Method

potman100

Active Member
Licensed User
Hi Guys

Just wondered if any one has any idea of how to send the OPTIONS method on a httpjob ?

I have searched the forum, but can't find anything on it ?

Thanks

Potman100
 

Erel

Administrator
Staff member
Licensed User
Example:
B4X:
Sub Process_Globals
   Private hc As OkHttpClient
   Private out As OutputStream
End Sub

Sub Service_Create
   hc.Initialize("hc")
   Send("https://www.b4x.com")
End Sub

Public Sub Send (URL As String)
   Dim req As OkHttpRequest
   Dim builder As JavaObject
   builder.InitializeNewInstance("okhttp3.Request.Builder", Null)
   builder.RunMethodJO("url", Array(URL)).RunMethod("method", Array("OPTIONS", Null))
   Dim jo As JavaObject = req
   jo.SetField("builder", builder)
   
   hc.Execute(req, 0)
End Sub

Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   Log("success")
   Log(Response.GetHeaders) 'ignore
   out.InitializeToBytesArray(0)
   Response.GetAsynchronously("res", out, False, 0)
End Sub

Sub res_StreamFinish (Success As Boolean, TaskId As Int)
   If Success Then
       Dim b() As Byte = out.ToBytesArray
       Log(BytesToString(b, 0, b.Length, "utf8"))
   End If
End Sub

Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log("error")
End Sub
 

potman100

Active Member
Licensed User
Yes, I searched the forum, and as this is not a httpjob it a OkHttpRequest your suggestion does not work, the req is not initialized, and there is no method for the options request as Erel has posted above to initialize it.
 
Last edited:

DonManfred

Expert
Licensed User
Code found using google:
B4X:
HttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.get()
  .setUri(SAMPLE_URL)
  .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
  .build();
client.execute(request);
You probably need to use javaobject to set it.
 

potman100

Active Member
Licensed User
Thanks for that, I did search online, but I am unsure of how to format the request in B4A.
 
Top