Forum Discussion

thialessandro's avatar
thialessandro
Frequent Visitor
3 years ago

API Connection

Hello,

 

I need to connect in API with method POST and authorization token, but the response is 404 or 400.

I tried with M language:

 

let
Json = Json.FromValue({[idType= "Authorization", idValue= "KEY123"]}),
Source = Json.Document(Web.Contents("https://apiintegracao.milvus.com.br/api/chamado/listagem", [Headers=[#"Content-Type"="application/json"]]))
in
Source

 

And I tried with Web Data Source, and not works?

The API link I'm trying to connect:

https://developers.milvus.com.br/#api-Chamado-listagemChamados

 

Anyone can help me, please?

 

Sorry about my level English.

3 Replies

  • karthiknc's avatar
    karthiknc
    Frequent Visitor

    Authorization also needs to be added to the Header. Please try again after making the header changes

  • thialessandro's avatar
    thialessandro
    Frequent Visitor

    I tried, but not works too.

     

    Now, I'm trying this way:

     

     

     

    let
    content = "{""filtro_body"": ""123""}",
    
    
    Source = Json.Document(Web.Contents("https://apiintegracao.milvus.com.br/api/chamado/listagem",
                           [Headers=[#"Authorization"="token", 
                                     #"apikey" = "****", 
                                     #"filtro_body" = "{}",
                                     #"ContentType"="application/json"], 
                                      
                            ManualStatusHandling = {404, 400},
                            
                           
                            Content=Text.ToBinary(content)]))
    
    in
    Source

     

     

     

    And not works too, but show me another message "Parameter not informed in the request body => {filtro_body}"

     

    I added the parameter 'filtro_body", but the message remains

     

  • Use a Python script being read directly by Power BI to query the API. Refer to the API https://developers.milvus.com.br/#api-Chamado-listagemChamados documentation to modify the parameters and the filter body ('filtro_body')

    import requests
    import pandas as pd
    import json

    url = "https://apiintegracao.milvus.com.br/api/chamado/listagem"

    # Add Query Param to the URL
    url += "?total_registros=1000"

    payload = json.dumps({
    "filtro_body": {
    "status": 4,
    }
    })
    headers = {
    'Content-Type': 'application/json',
    'Authorization': '123token'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    # Parse the JSON response
    data = response.json()

    # Check if the response was successful
    if 'lista' in data:
    # Convert the response data to a DataFrame
    df = pd.DataFrame(data['lista'])

    # Select and organize columns
    columns_to_select = ["codigo", "cliente", "contato", "categoria_primaria", "categoria_secundaria", "tecnico", "total_horas", "data_criacao", "data_solucao", "servico_realizado"]
    if all(col in df.columns for col in columns_to_select):
    df = df[columns_to_select]

    # Format date columns, if necessary
    df["data_criacao"] = pd.to_datetime(df["data_criacao"])
    df["data_solucao"] = pd.to_datetime(df["data_solucao"])

    # Display DataFrame in tabular format
    print(df.to_string(index=False))
    else:
    print("Some specified columns are not present in the DataFrame returned by the API.")
    # Add logic here to handle the situation of missing columns, if necessary.
    else:
    print("Error in API response:", data)