B4J Question Call rest API

imbault

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

I need to call an API using this curl example :

B4X:
curl "https://api.safetyculture.io/audits/audit_01ca38a821504cda885736cccbb9ba40/media/9E3BD015-6275-4668-BAF1-296B2F38444C" \
  -o 9E3BD015-6275-4668-BAF1-296B2F38444C.jpg \
  -H "Authorization: Bearer ..."
Doc says :
The media is downloaded directly, so you should save the output to a file with an appropriate name.
HTTP Request
GET https://api.safetyculture.io/audits/<audit ID>/media/<media ID>

URL Parameters
Parameter Description
audit ID The ID of the audit to retrieve
media ID The ID of the media within the audit to retrieve
Response
The Content-Type will be the MIME type associated with the media, and the body of the response is the media itself.

So I tried :

B4X:
    Dim job As HttpJob
    CUrl = "https://api.safetyculture.io"
    Dim cPost ="/audits/audit_3ec7a77d89544c958c0c8e1a73157cd9/media/ad1133f5-dfd0-4b93-a61d-4f45fddfbd91" As String
    cMedia = "2a5b04e7-d4ed-4de4-ba28-5d26e63e40b2.jpg"
    
    job.Initialize("",Me)
    job.PostString(CUrl, cPost)
    job.GetRequest.SetContentType("image/jpeg")
    job.GetRequest.SetHeader("authorization", "Bearer " & cIOToken)
    
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        job.GetString()
    End If

And job.sucess is always false

Any idea how to call this API?

Thanks a lot

Patrick
 

udg

Expert
Licensed User
Longtime User
Dim cPost ="/audits/audit_3ec7a77d89544c958c0c8e1a73157cd9/media/ad1133f5-dfd0-4b93-a61d-4f45fddfbd91" As String
cMedia = "2a5b04e7-d4ed-4de4-ba28-5d26e63e40b2.jpg"

Shouldn't it be:
B4X:
 Dim cPost ="/audits/audit_3ec7a77d89544c958c0c8e1a73157cd9/media/2a5b04e7-d4ed-4de4-ba28-5d26e63e40b2" As String
 cMedia = "2a5b04e7-d4ed-4de4-ba28-5d26e63e40b2.jpg"

I mean, trailing chars for CPost being the mediaID as shown in cMedia but without its extension.
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Thanks, but no, I tried and doesn't work, and I've no problem sending the request with SoapUI...
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Is there any chance that the SetHeader should be written like :
job.GetRequest.SetHeader("Authorization:", "Bearer " & cIOToken)
i.e. capital letter and eventually a trailing colon?

I'm not a curl expert, so others will be more useful here. Just spotted a couple of banal points that eventually could be the cause (not betting a dime on them).
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Thanks, but job.GetRequest.SetHeader("Authorization:", "Bearer " & cIOToken) is just fine, I already use it in another method of this API
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Got it :

B4X:
    Dim job As HttpJob
    Dim Curl = "https://api.safetyculture.io" as String
    Dim cPost ="/audits/audit_3ec7a77d89544c958c0c8e1a73157cd9/media/ad1133f5-dfd0-4b93-a61d-4f45fddfbd91" As String
'    CUrl "https://api.safetyculture.io/audits/audit_01ca38a821504cda885736cccbb9ba40/media/9E3BD015-6275-4668-BAF1-296B2F38444C" \
'  -o 9E3BD015-6275-4668-BAF1-296B2F38444C.jpg \
'  -H "Authorization: Bearer ..."
    
    job.Initialize("",Me)
    job.Download(Curl &  cPost  )
    job.GetRequest.SetHeader("authorization", "Bearer " & cIOToken)
    
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Log("good media retrieve")
        Log(job.GetString)

    End If
    job.Release
 
  • Like
Reactions: udg
Upvote 0
Top