B4J Question jHttp / jHttpUtils2 PostString versus jOKHttp / jOKhttputils2 - Index out of bound

notedop

Member
Licensed User
Longtime User
Hi,

I'm trying to post a JSON body using poststring and manually setting the headers of the http post.
When I use jHttp / jHttpUtils2 that I am able to succesfully make the post and get a response which is good.
When I use the exact same code on jOKHttp / jOKhttputils2 I am getting an "Index was outside the bounds of the array." error in the logs.

jHttp ; Version 1.11
jHttpUtils2 : Version 1.03
jOKHttp : Version 1.20
jOKhttputils2 : Version 2.40

I've downloaded the latest version of B4J.

I want to use the jOKhttp including Utils as it is advised to utilize this version.
Next thing is that I need to capture the response cookies. I've found an instruction to update the library for OKhttpUtils2, but it seems that the available sourcecode is not the latest version.

Questions:
1. Why am I getting Index was outside the bounds of the array issue when using jOKhttp and utils2 with the exact same code. Is this a bug?
2. Can someone direct me the absolute latest source code of the OK libraries so I can update to capture the response headers.

I've attached the projects using both versions to show the difference.

B4X:
Sub Submit_Action
  
    Dim j As HttpJob
    j.Initialize("downloadFile",Me )

    j.PostString("http://comicspriceguide.com/services/siteSearch.asmx/returnTitleSearch","{'sec':{'hs':'27m56uzkeCd2JL//EXWIxyYRV7fkQf9aBzwv6HIpK7k=','ts':'leBEam8WunoHO37PaGhgJAnnxTslcY/XWTroHKA2Cm8='},'filter':{'titleSearch':'walking dead','issueNu':'','country':'MirOu88YWYXjov%2bGDAjeSw%3d%3d','orderType':'pop','searchType':'allwords','searchTabs':'titles','page':1,'per_page':50,'direction':'asc','column':''},'config':{'uid':'G4/NHo2Wlu/T/8lOwpGVEg==','sec':'RBf68jNcrhE8xM50fCudcQ==','schk':['titleSearch','issueNu'],'rarr':['searchType','searchTabs']}}")
    j.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
    j.GetRequest.Setheader("Referer", "http://comicspriceguide.com/comic-book-search")
    j.GetRequest.SetHeader("Origin", "http://comicspriceguide.com")
    j.GetRequest.SetHeader("Content-Type", "application/json; charset=UTF-8")
    j.GetRequest.SetHeader("Accept", "application/json, text/javascript, */*; q=0.01")
    j.GetRequest.SetHeader("Accept-Encoding", "gzip, deflate")
    j.GetRequest.SetHeader("Accept-Language", "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4")
    j.GetRequest.SetHeader("Cookie","cpgCheck=; ")

End Sub


Sub JobDone(job As HttpJob)
  
    Log(job.Tag)
    If job.Success Then
        Log(job.JobName & " - " & job.GetString)
        '        Dim m As Map = job.ResponseHeaders
        '
        '        For i = 0 To m.Size - 1
        '             Dim Key, Value As String
        '             Key = m.GetKeyAt(i)
        '             Value = m.GetValueAt(i)
        '             Log(job.JobName & " - Key:" & Key & ":Value" & Value)
        '        Next
    End If
    job.Release

End Sub

JHTTP log (GOOD RESPONSE!) Job.sucess = true

B4X:
Waiting for debugger to connect...
Program started.
java.lang.Object@17a1c670
downloadFile - {"d":{"code":-1,"error":["\u003cdiv class=\u0027clearfix\u0027\u003e\u003ci class=\u0027icon-warning-sign icon-3x pull-left marginleft\u0027\u003e\u003c/i\u003e Your session has expired.  Please \u003ca href=\u0027/login\u0027\u003esign in\u003c/a\u003e again to continue.\u003c/div\u003e"],"refresh":true,"page":0,"pages":0,"perPage":0,"total":0,"status":"ERR","loggedin":0}}
java.lang.Object@20c7fa81
downloadFile - {"d":{"code":-1,"error":["\u003cdiv class=\u0027clearfix\u0027\u003e\u003ci class=\u0027icon-warning-sign icon-3x pull-left marginleft\u0027\u003e\u003c/i\u003e Your session has expired.  Please \u003ca href=\u0027/login\u0027\u003esign in\u003c/a\u003e again to continue.\u003c/div\u003e"],"refresh":true,"page":0,"pages":0,"perPage":0,"total":0,"status":"ERR","loggedin":0}}

JOKHTTP log: (ERROR RESPONSE HANDLING). Job.sucess = false

B4X:
Waiting for debugger to connect...
Program started.
Index was outside the bounds of the array.
java.lang.Object@3ae1ba41
 

Attachments

  • JOKHttpUtils Poststring.zip
    2.8 KB · Views: 404
  • JhttpUtils Poststring.zip
    2.9 KB · Views: 422
Last edited:

DonManfred

Expert
Licensed User
Longtime User
The error is
Internal Server Error
when running into the job.success = false

The Server crashes with your request. Seems like the problem is inside the server or your postreqest is wrong.
 
Upvote 0

notedop

Member
Licensed User
Longtime User
The Server crashes with your request. Seems like the problem is inside the server or your postreqest is wrong.

That was my initial thought as well. That's why I made 2 projects with the EXACT SAME code, but utilizing different libraries.
The httputils works, the Okhttp does not.

This implies that both libraries are using the data differently and building the request differently.

After having another look with new breadth of perspective;

I think I might have found the issue. Adding the following statement when using OKHttp now results in success response.

B4X:
j.GetRequest.SetContentType("application/json; charset=UTF-8")

The strange thing is that I was already setting the content type by the following:

B4X:
j.GetRequest.SetHeader("Content-Type", "application/json; charset=UTF-8")

I found this while verifying the J object --> Req --> Builder --> Body --> ContentType of the OKhttp build and noticed that it did not adapt the content-type provided by the setheader method.

This means that using OKhttp you need to explicitly use the setcontenttype method and it does not suffice setting a content-type header via the setheader method.
 
Upvote 0

notedop

Member
Licensed User
Longtime User
Did another final test to proof this by using http://httpbin.org/post. This website responds with the header information in json format.
As you can see below when manually setting the content type header using OKhttp, the content-type is not used.

B4X:
j.PostString("http://httpbin.org/post", "{'test':'data2'}")

j.GetRequest.SetHeader("Content-Type", "application/json; charset=UTF-8")

Waiting for debugger to connect...
Program started.
java.lang.Object@3a4e9c9f
[jobname=downloadFile, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@5b661a97, tag=java.lang.Object@3a4e9c9f
, fx=anywheresoftware.b4j.objects.JFX@4362cb6e, httputils2service=null]
downloadFile - {
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "{'test':'data2'}": ""
  },
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "16",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "okhttp/3.8.0"
  },
  "json": null,
  "origin": "",
  "url": "http://httpbin.org/post"
}

Explicit contenttype setting

B4X:
j.PostString("http://httpbin.org/post", "{'test':'data2'}")
j.GetRequest.SetContentType("application/json; charset=UTF-8")

java.lang.Object@11b71566
[jobname=downloadFile, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=2, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@15ea4bc4, tag=java.lang.Object@11b71566
, fx=anywheresoftware.b4j.objects.JFX@2e84fe66, httputils2service=null]
downloadFile - {
  "args": {},
  "data": "{'test':'data2'}",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "16",
    "Content-Type": "application/json; charset=UTF-8",
    "Host": "httpbin.org",
    "User-Agent": "okhttp/3.8.0"
  },
  "json": null,
  "origin": "",
  "url": "http://httpbin.org/post"
}
 
Upvote 0
Top