Forum Discussion

Jules88's avatar
Jules88
New Member
2 years ago
Solved

API call with JSON formatted body

Hello,

 

I am looking for a little help with regards to an API call I am trying to make and I cannot quite get it to work.

 

 

let

    AccessToken="Bearer "&GetAuthToken(),

    url="https://gateway.qg2.apps.qualys.eu/rest/2.0/search/am/asset",

    body = "
        {
            ""filters"":
            [
                {
                ""field"":""software.name"",
                ""operator"":""contains"",
                ""value"":""Chrome""
                }
            ]
        }",

    headers=[#"Content-Type"="application/json",#"X-Requested-With"="PowerBi",#"Accept"="*/*",Authorization=AccessToken],

    Source = Web.Contents(url,[Content=Text.ToBinary(body),Headers=headers,Query=[#"includeFields"="hardware"]]),

 

 

If I use powershell, I can get this to work using a similar format. 

 

 

#Create our search criteria.
$body = @"
{
    "filters": [
    {
    "field": "software.name",
    "operator": "CONTAINS",
    "value": "Chrome"
    }
    ]
   }
"@

#Now we will go and get the first set of records
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-Requested-With", "Postman")
$headers.Add("Content-Type", "application/json")
$headers.Add("Accept", "*/*")
$headers.Add("Authorization", "Bearer "+ $authtoken)

$response = Invoke-RestMethod 'https://gateway.qg2.apps.qualys.eu/rest/2.0/search/am/asset?includeFields=hardware' -Method 'POST' -Headers $headers -Body $body

 

 

I suspect I have done something silly or made a typo, but I just cannot get this to work in PowerBi. I keep getting a http 400 error.

 

DataSource.Error: Web.Contents failed to get contents from 'https://gateway.qg2.apps.qualys.eu/rest/2.0/search/am/asset?includeFields=hardware' (400): Bad Request
Details:
DataSourceKind=Web
DataSourcePath=https://gateway.qg2.apps.qualys.eu/rest/2.0/search/am/asset
Url=https://gateway.qg2.apps.qualys.eu/rest/2.0/search/am/asset?includeFields=hardware

 

Any insight would be hugely appreciated.

 

Thanks

 

John

  • Actually managed to get this sorted myself, thanks anyway

  • The code itself appeared to be correct, maybe it was the formatting that needs to be all on one line, but the operator is case sensitive, has to be in capitals, certainly for the API call I was using anyway.

     

        body="{ ""filters"": [{""field"":""software.name"", ""operator"":""EQUALS"", ""value"":""Chrome""}]}"

5 Replies

  • Actually managed to get this sorted myself, thanks anyway

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Jules88 ,

      Glad to hear you resolved the issue yourself!
      By the way, maybe there are other users who are facing the same problem as you, could you please post your solution below and accept it as a solution, it will make it easier for other users to find the answer faster, thanks!

      Best Regards,
      Dino Tao

  • The code itself appeared to be correct, maybe it was the formatting that needs to be all on one line, but the operator is case sensitive, has to be in capitals, certainly for the API call I was using anyway.

     

        body="{ ""filters"": [{""field"":""software.name"", ""operator"":""EQUALS"", ""value"":""Chrome""}]}"
  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello,

    I'm trying to get the Bearer Token from Qualys API, I've seen that you have already done this with function

    GetAuthToken()

    This is my script

    let
    url = "https://gateway.qg2.apps.qualys.eu/auth",
    headers = [#"Authorization" = "Bearer {{authTokenValue}}",#"Content-Type" = "application/x-www-form-urlencoded", #"Accept" = "*/*", #"X-Requested-With"="QualysPostman"],
    postData = Json.FromValue(
    [
    #"username" = "xxxxx",#"password" = "xxxx",#"token" = "true"
    ]),
    response = Web.Contents(url,
    [
    Headers = headers,
    Content = postData
    ]),
    jsonResponse = Json.Document(response)
    in
    jsonResponse

     

    but I receive teh following error message: 

    DataSource.Error: Impossibile per Web.Contents ottenere il contenuto da 'https://gateway.qg2.apps.qualys.eu/auth' (500): Internal Server Error
    Dettagli:
    DataSourceKind=Web
    DataSourcePath=https://gateway.qg2.apps.qualys.eu/auth
    Url=https://gateway.qg2.apps.qualys.eu/auth

     

    Could you help me to solve the problem?

    Thanks in advance

     

    Francesco

    • Jules88's avatar
      Jules88
      New Member

      Hello fmagli,

       

      Sorry for the delay in replying.

       

      I think you are calling the Auth API to get the bearer token, so you shouldn't need the Authorization header at this stage, that would be used when you are calling other API's for data.

       

      My authtoken looks like

       

      let
          Source = () => let
      
              Source = Text.FromBinary(Web.Contents("https://gateway.qg2.apps.qualys.eu/auth",
                  [
                      Headers=[#"Content-Type" = "application/x-www-form-urlencoded"],
                      Content = Text.ToBinary("username=xxxxxxx&password=xxxxxxxx&token=true")
                  ])),
              AuthResponse=Json.Document(Source)
              
          in
              Source
      in  
          Source

      and the authreponse is then called later.

       

      Does that help you?