B4J Question call API rest

imbault

Well-Known Member
Licensed User
Longtime User
Hi B4X team,

I have to call a API with this curl example:
B4X:
curl -X POST "https://api.safetyculture.io/audits" \
  -d '{"template_id": "template_BB29F82814B64F559A33BF7CAA519787"}' \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ..."

this call should retrieve a Json string, here is an example :

B4X:
curl -X POST "https://api.safetyculture.io/audits" \
  -d '{
        "template_id": "template_b043f583f34642f4b446f46eeb5405dd",
        "header_items": [
          {
            "item_id": "f3245d42-ea77-11e1-aff1-0800200c9a66",
            "label": "Conducted on",
            "type": "datetime",
            "responses": {
              "datetime": "2016-12-10T09:08:55+00:00"
            }
          }
        ]
      }' \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ..."

I don't know how to instanciate an HttpJob object and set the headers

Any help should be highly appreciated
 

DonManfred

Expert
Licensed User
Longtime User
something like

B4X:
    Dim job As HttpJob
    job.Initialize("getaccount",Me)
    job.PostString("https://api.safetyculture.io/audits",$"{"template_id": "template_b043f583f34642f4b446f46eeb5405dd","header_items": [{"item_id": "f3245d42-ea77-11e1-aff1-0800200c9a66", "label": "Conducted on","type": "datetime","responses": {"datetime": "2016-12-10T09:08:55+00:00"}}]}"$)
    job.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken)
    job.GetRequest.SetHeader("Content-Type", "application/json")
    job.GetRequest.SetContentType("application/json")
    job.GetRequest.SetContentEncoding("text/plain")
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Thank you, but I'm just trying the method :

B4X:
curl \
  -X POST \
  -d [email protected] \
  -d password=secret \
  -d grant_type=password \
  "https://api.safetyculture.io/auth"

with

B4X:
    Dim job As HttpJob
    job.Initialize("getaccount",Me)
    job.PostString("https://api.safetyculture.io/auth","[email protected] password=xxx grant_type=password")
    job.GetRequest.SetHeader("Content-Type", "application/json")
    job.GetRequest.SetContentType("application/json")

And I receive :
{"statusCode":415,"error":"Unsupported Media Type","message":"Unsupported Media Type"}

Any idea?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Don't set the Content-Type header.

2. The content type is not json. Try it with:
B4X:
Dim job As HttpJob
    job.Initialize("getaccount",Me)
    job.PostString("https://api.safetyculture.io/auth","[email protected]&password=xxx&grant_type=password") 'added &
    job.GetRequest.SetContentType("application/x-www-form-urlencoded")
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
something like

B4X:
    Dim job As HttpJob
    job.Initialize("getaccount",Me)
    job.PostString("https://api.safetyculture.io/audits",$"{"template_id": "template_b043f583f34642f4b446f46eeb5405dd","header_items": [{"item_id": "f3245d42-ea77-11e1-aff1-0800200c9a66", "label": "Conducted on","type": "datetime","responses": {"datetime": "2016-12-10T09:08:55+00:00"}}]}"$)
    job.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken)
    job.GetRequest.SetHeader("Content-Type", "application/json")
    job.GetRequest.SetContentType("application/json")
    job.GetRequest.SetContentEncoding("text/plain")

Works perfectly Manfred, thanks a lot, just a question, why do you enclose the string with $ in that case?
B4X:
$"{"template_id": "template_b043f583f34642f4b446f46eeb5405dd","header_items": [{"item_id": "f3245d42-ea77-11e1-aff1-0800200c9a66", "label": "Conducted on","type": "datetime","responses": {"datetime": "2016-12-10T09:08:55+00:00"}}]}"$
 
Upvote 0
Top