B4J Question How to post "raw" with okhttputils?

MathiasM

Active Member
Licensed User
Hello

I wanted to use the OkHTTPUtils library to POST to an API.
This is the code I use:

B4X:
Dim j As HttpJob
    j.Initialize("", Me)
    Dim json As JSONGenerator
    json.Initialize(CreateMap("Username":"admin", "Password":"admin"))
    j.PostString("http://localhost:10601/session", json.ToString)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release

This works, but when I check the transmitted packets via Wireshark I notice that the data is not send "raw" but rather via x-www-form-urlencoded.

How can I POST a string to a URL "raw", or how to set the content-type of a POST request?

Thanks
 
Solution
I fixed it:

B4X:
j.PostString("http://localhost:10601/session", json.ToString)
j.GetRequest.SetContentType("application/json")

Previously I was trying to set the content type before the actual PostString method. Apparently it needs to be set after the PostString method.

MathiasM

Active Member
Licensed User
I fixed it:

B4X:
j.PostString("http://localhost:10601/session", json.ToString)
j.GetRequest.SetContentType("application/json")

Previously I was trying to set the content type before the actual PostString method. Apparently it needs to be set after the PostString method.
 
Upvote 1
Solution

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0
Top