B4J Question OKHTTP REQUEST IN B4X HELP.

jahswant

Well-Known Member
Licensed User
Longtime User
I have a series of 4 request on postman that are working but I can't seem to figure out how to make them work in b4x.

Java:
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
  .url("https://xx.xx.xx")
  .method("POST", body)
  .addHeader("Key", "123456789")
  .addHeader("Authorization", "Basic ")
  .build();
Response response = client.newCall(request).execute();

I tried this but I get a negative response from server.

B4X:
    Dim j As HttpJob
    j.Initialize("",Me)
    j.Download("https://xx.xx.xx")
    j.GetRequest.SetHeader("Key", "123456789")
    j.GetRequest.SetHeader("Authorization", "Basic ")
    wait for (j) JobDone(job As HttpJob)
    If job.Success Then
        Log(job.GetString)
    Else
        Log(job.ErrorMessage)
    End If

These are other requests :

Java:
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
  .url("https://xxx.xxx.xxx")
  .method("POST", body)
  .addHeader("env", "")
  .addHeader("Key", "123456")
  .addHeader("Authorization", "Bearer ")
  .addHeader("Id", "")
  .addHeader("Content-Type", "application/json")
  .build();
Response response = client.newCall(request).execute();


Java:
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
Request request = new Request.Builder()
  .url("https://xxx.xxx.xxx")
  .method("GET", null)
  .addHeader("env", "")
  .addHeader("Key", "123456789")
  .addHeader("Authorization", "Bearer ")
  .build();
Response response = client.newCall(request).execute();
 
Solution
The first one should be:
B4X:
  Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://xx.xx.xx", "")
    j.GetRequest.SetHeader("Key", "123456789")
    j.GetRequest.SetHeader("Authorization", "Basic ")
    j.GetRequest.SetContentType("text/plain")
    wait for (j) JobDone(job As HttpJob)

Erel

B4X founder
Staff member
Licensed User
Longtime User
The first one should be:
B4X:
  Dim j As HttpJob
    j.Initialize("",Me)
    j.PostString("https://xx.xx.xx", "")
    j.GetRequest.SetHeader("Key", "123456789")
    j.GetRequest.SetHeader("Authorization", "Basic ")
    j.GetRequest.SetContentType("text/plain")
    wait for (j) JobDone(job As HttpJob)
 
Upvote 1
Solution
Top