B4J Question When to Use SetContentType or SetHeader("Content-Type", ...)

mcqueccu

Well-Known Member
Licensed User
Longtime User
Hi, Am using jOkhttputils2 to call an API

The main transaction method uses POST and the call was successful whiles using setContentType to set the header

But when using GET request to check the status of the transaction
setContentType keeps giving me an error. Meanwhile setHeader("Content-Type","application/json") works fine with the GET.

When is it right to use SetContentType and when is it right to use setHeader

Endpoint: POST
https://api.xxxxxx.com/v1/receive

Header data value
apikey xxxxxxxxxxxxxxxxxxxx
Content-Type application/json

B4X:
   Dim ok As HttpJob
    ok.Initialize("",Me)
    
    ok.PostString(endpointurl,payload)    
    ok.GetRequest.SetHeader("ApiKey",apikey)    'set the header with api key
    ok.GetRequest.SetContentType("application/json")    'set the header as json
    ok.GetRequest.SetContentEncoding("UTF8")        'set encoding as utf8



Endpoint: GET
https://api.xxxxxxxx.com/v1/status/{transactionid}

Header data Value
apikey xxxxxxxxxxxxxxxxxxxx
appid xxxx
Content-Type application/json

B4X:
    Dim jj As HttpJob
    jj.Initialize("",Me)
    
    jj.Download(checkUrl)   
    jj.GetRequest.SetHeader("ApiKey",apikey)   
    jj.GetRequest.SetHeader("appid",appid)
    jj.GetRequest.SetHeader("Content-Type","application/json") 'working with GET Method
'    jj.GetRequest.SetContentType("application/json")    'not working with GET method but works with POST
    jj.GetRequest.SetContentEncoding("UTF8")

Error Message whiles using SetContentType

B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 235 (HttpJob)
java.lang.RuntimeException: Request does not support this method.
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest.SetContentType(OkHttpClientWrapper.java:493)
    at b4j.example.main$ResumableSub_AppStart.resume(main.java:138)
    at b4j.example.main._appstart(main.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at b4j.example.main.start(main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
 
Top