B4J Question How to use jOkHttpUtils2 to post form-data?

kimstudio

Active Member
Licensed User
Longtime User
I can't find an example to post form-data to login and get authorization code using jOkHttpUtils2. Thanks for help!

Post address: http://192.168.100.2/EHCommon/admin/user/login

Headers:
Content-Type: application/x-www-form-urlencoded

Body form-data:
http_api_version: 4.16.1
username: admin
password: xxxxxx

This is the Postman catpure that can get right response:
post.PNG



This is the java code converted by Postman, but I don't know how to convert MultipartBody part to B4J:

Java:
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("http_api_version","4.16.1")
  .addFormDataPart("username","admin")
  .addFormDataPart("password","dc047c30ebf9dac462908aadad719dce")
  .build();
Request request = new Request.Builder()
  .url("http://192.168.100.2/EHCommon/admin/user/login")
  .method("POST", body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .build();
Response response = client.newCall(request).execute();
 
Solution
not tested
note:

check request response type
B4X:
Public Sub test_HttpPostMultipart
    Dim URL As String = "http://192.168.100.2/EHCommon/admin/user/login"
    Dim Parameters As Map = CreateMap( "http_api_version" : "4.16.1", "username" : "admin", "password" : "dc047c30ebf9dac462908aadad719dce")
    Wait For (HttpPostMultipart(URL, Parameters)) Complete (Result As String)
    Log(Result)
End Sub

Public Sub HttpPostMultipart(Link As String, Parameters As Map) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostMultipart(Link, Parameters, Null)
        j.GetRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded")
        Wait For (j) JobDone(j As HttpJob)...

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
look at the example here:

The method is:
j.PostMultipart
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I can't find an example to post form-data to login and get authorization code using jOkHttpUtils2. Thanks for help!

Post address: http://192.168.100.2/EHCommon/admin/user/login

Headers:
Content-Type: application/x-www-form-urlencoded

Body form-data:
http_api_version: 4.16.1
username: admin
password: xxxxxx

This is the Postman catpure that can get right response:
View attachment 139406


This is the java code converted by Postman, but I don't know how to convert MultipartBody part to B4J:

Java:
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("http_api_version","4.16.1")
  .addFormDataPart("username","admin")
  .addFormDataPart("password","dc047c30ebf9dac462908aadad719dce")
  .build();
Request request = new Request.Builder()
  .url("http://192.168.100.2/EHCommon/admin/user/login")
  .method("POST", body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .build();
Response response = client.newCall(request).execute();


B4X:
dim client as httpjob
client.initalize("cli", me)
'client.PostString("http://192.168.100.2/EHCommon/admin/user/login", "username=admin&password=xxxx")
'or try this
client.PostMultipart("http://192.168.100.2/EHCommon/admin/user/login", CreateMap("username": "admin", "password":"xxxxx" ), Null)

You also refer to this
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
not tested
note:

check request response type
B4X:
Public Sub test_HttpPostMultipart
    Dim URL As String = "http://192.168.100.2/EHCommon/admin/user/login"
    Dim Parameters As Map = CreateMap( "http_api_version" : "4.16.1", "username" : "admin", "password" : "dc047c30ebf9dac462908aadad719dce")
    Wait For (HttpPostMultipart(URL, Parameters)) Complete (Result As String)
    Log(Result)
End Sub

Public Sub HttpPostMultipart(Link As String, Parameters As Map) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostMultipart(Link, Parameters, Null)
        j.GetRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString 'the request response type?
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
 
Upvote 0
Solution

teddybear

Well-Known Member
Licensed User
Hi Teddybear, I will test later but I didn't see "form-data" anywhere in your code.
It will be generated by passing parameters CreateMap("username": "admin", "password":"xxxxx" ) and Null
 
Upvote 0
Top