Android Question Getting "Bad Request" in posting JSON Data to an Endpoint

lnxpy

New Member
Hey guys, hope you're doing well.
I want to create an application to post some JSON data to an endpoint and get a field from the returned response. Whenever I click the button it takes back with the "Bad Request" error and says that two "title" and "script" fields are required. (I've already filled them up)
It really ticks me off. The endpoint takes two required fields. Title and Script. (It also takes some optional fields)
In spite, I made a completed JSON variable, it does not work.

Here is my code:
B4X:
Dim data As Map = CreateMap("title": edt_title.Text, "detail": edt_detail.Text, "script": edt_script.Text, "language": ac_language.Text)
        
Dim json As JSONGenerator
json.Initialize(data)
        
Dim connection As HttpJob
connection.Initialize("Connection", Me)
connection.PostString("http://codehub.pythonanywhere.com/api/v1/snippet/", json.ToString)
connection.GetRequest.SetHeader("Content-Type", "application/json")
ToastMessageShow("Created",True)

This is the scenario that I want to implement. The user should fill some fields and the program should post those data in a JSON-styled form to the endpoint. Then, the webserver responses back a JSON data and I need to chop it and take the "link" field from it.

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't set the content type like this. It will be overwritten.
Use connection.GetRequest.SetContentType(...).

Never use the job name parameter.
Use Wait For to catch the Job Done event.


Add the clear text flag: https://www.b4x.com/android/forum/threads/87610/#content
 
Upvote 0

lnxpy

New Member
Thank you Erel. It's for a few days I'm struggling with this stuff. Now it's completely fixed. Really appreciate it.
 
Upvote 0
Top