Android Question Restful API service

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

How to use http post request to implement Restful API service?
 

khwarizmi

Active Member
Licensed User
Longtime User
thanks.

I tried to make a post job, but was not succeeded:
B4X:
Dim JSONList As List
JSONList.Initialize
Dim ZeileMap As Map
ZeileMap.Initialize
ZeileMap.put("first_name": "Ali")
ZeileMap.put("last_name": "Adil")
ZeileMap.put("country_code": "974")
ZeileMap.put("mobile": "5514215437")
ZeileMap.put("email": "katt33ah@gmail.com")
ZeileMap.put("created_user_id": 1)
ZeileMap.put("created_time": "2019-04-07T17:41:37.000")
ZeileMap.put("updated_user_id": Null)
ZeileMap.put("updated_time": Null)
ZeileMap.put("pw": "ggg")
ZeileMap.put("is_active": "Y")
JSONList.Add(ZeileMap)
  Dim JSONGenerator As JSONGenerator
   JSONGenerator.Initialize2(JSONList)
   Dim str As String=JSONGenerator.ToString
 Dim j As HttpJob
    j.Initialize("ghg",Me)
 j.PostString("http://192.168.1.12:3000/customers", Array As String("MyJSON", str))

and this is the JobDone:

B4X:
Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Select Job.JobName
            Case "ghg"
                Dim res As String
                res = Job.GetString
                Log(res)
        End Select
    End If
End Sub

The response is : {"message":"ER_BAD_NULL_ERROR: Column 'first_name' cannot be null"} ,
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
this is the response:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token L in JSON at position 1<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt<br> &nbsp; &nbsp;at parse (C:\Users\Abuelhaytham\30\node_modules\body-parser\lib\types\json.js:89:19)<br> &nbsp; &nbsp;at C:\Users\Abuelhaytham\30\node_modules\body-parser\lib\read.js:121:18<br> &nbsp; &nbsp;at invokeCallback (C:\Users\Abuelhaytham\30\node_modules\raw-body\index.js:224:16)<br> &nbsp; &nbsp;at done (C:\Users\Abuelhaytham\30\node_modules\raw-body\index.js:213:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\Abuelhaytham\30\node_modules\raw-body\index.js:273:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:182:13)<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1091:14)<br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:174:19)</pre>
</body>
</html>
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
thanks, I changed it to the right form, but also I get an error Although it reads the JSON array correctly:

[{"first_name":"Ali","last_name":"Adil","country_code":"974","mobile":"5514215437","email":"katt33ah@gmail.com","created_user_id":1,"created_time":"2019-04-07T17:41:37.000","updated_user_id":null,"updated_time":null,"pw":"ggg","is_active":"Y"}]

the response is:
{"message":"ER_BAD_NULL_ERROR: Column 'first_name' cannot be null"}
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
I used Node.js to write the initial rest api mysql database code. and then tested it with the Postman. this is the resut in the Postman:
 

Attachments

  • post.png
    160.9 KB · Views: 213
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
i see your code in post #3 - you throw updated_user_id and updated_time with Null value as parameter while you don't in postman above, maybe you can try to replace Null value with data you use in the postman, is it make changes ?
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
i see your code in post #3 - you throw updated_user_id and updated_time with Null value as parameter while you don't in postman above, maybe you can try to replace Null value with data you use in the postman, is it make changes ?

thanks, In fact, in my last attempts I tried to give values to all fields.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
The JSON array formula in Postman begins with { and ends with } as in the picture.
I tried to modify the string and remove [ and ] but was not succeeded.
all my problem is to post the JSON array in the data, because I always get the error:

{"message":"ER_BAD_NULL_ERROR: Column 'first_name' cannot be null"}

Which indicates that the JSON array was not read .
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Not sure how you remove the [ and ]. Instead of parsing as List, you have to parse json from Map.

B4X:
    Dim ZeileMap As Map
    ZeileMap.Initialize
    ZeileMap.put("first_name", "Ali")
    ZeileMap.put("last_name", "Adil")
    ZeileMap.put("country_code", "974")
    ZeileMap.put("mobile", "5514215437")
    ZeileMap.put("email", "katt33ah@gmail.com")
    ZeileMap.put("created_user_id", 1)
    ZeileMap.put("created_time", "2019-04-07T17:41:37.000")
    ZeileMap.put("updated_user_id", Null)
    ZeileMap.put("updated_time", Null)
    ZeileMap.put("pw", "ggg")
    ZeileMap.put("is_active", "Y")
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(ZeileMap)
    Dim str As String = JSONGenerator.ToString
    Dim j As HttpJob
    j.Initialize("ghg",Me)
    'Log(str)
    j.PostString("http://192.168.1.12:3000/customers", str)
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
I modified the code as you mentioned, and tried to add:
B4X:
str=str.Replace("[","")
    str=str.Replace("]","")
but the problem remained the same.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
I used your code as it was at the beginning, then tried to make the modification.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…