Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

API header in Web.Contents is not respected.

Need to make API GET request with following headers especially 'q' search term which i but this doesn't seem to be respecting it. Can anyone help me out on this, if the following code has a mistake in it? The API header here is coming from an excel file and API is executing on it. 

The search results with 'URL' in the JSON response which should be different for search terms (i.e., q) but here it doesn't seem to be respecting it. 

let
    Query2 = let
    Source = Excel.Workbook(File.Contents("C:\Users\nitin.chawda\Documents\InputSearchIRDText.xlsx"), null, true),
        SourceData_Table = Source{[Item="Sheet1",Kind="Sheet"]}[0][Data],
       #"Filtererd_Rows" = Table.TransformColumns(SourceData_Table)
  in Source,
    Sheet1_Sheet = Query2{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Search_Term", type text}, {"Search_Scope", type text}, {"Type_of_Facet", type text}, {"Page_Number", type text}, {"Item_id", type text}}),
//Start custom function to read records in table. Here is where the error is.

apiLoadSheetFunction = (_ as record) =>

let
    url = "https://www.ird.govt.nz//sxa/search/results/",
    headers = [
        #"Content-Type" = "application/json",
        #"q" =  " & Text.From(_[Search_Term]) & ",
        #"s" = " & Text.From(_[Search_Scope]) & ",
        #"type" = " & Text.From(_[Type_of_Facet]) &",
        #"itemid" = " & Text.From(_[Item_id]) &",
        #"e" = "& Text.From(_[Page_Number]) &"],
	webdata = Web.Contents(url, [Headers=headers, ManualStatusHandling={400..499}]),
    response = Json.Document(webdata),
    Source = response

in 
    response,
    
	AStepName = Table.AddColumn(#"Changed Type", "APICall", each apiLoadSheetFunction(_)),
    #"Expanded APICall" = Table.ExpandRecordColumn(AStepName, "APICall", {"Results"}, {"APICall.Results"}),
    #"Expanded APICall.Results" = Table.ExpandListColumn(#"Expanded APICall", "APICall.Results"),
    #"Expanded APICall.Results1" = Table.ExpandRecordColumn(#"Expanded APICall.Results", "APICall.Results", {"Url"}, {"APICall.Results.Url"})
in
    #"Expanded APICall.Results1"

 Something wrong with above code?

1 ACCEPTED SOLUTION
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @Anonymous ,

Firstly, do you get any error message with the M query?

In addition, do you only have the issue with 'q' search term ? What about 's' search_scope?

What's more, you could have a reference of the link below.

https://community.powerbi.com/t5/Desktop/Retrieve-API-response-headers/td-p/423704

Best  Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @Anonymous ,

Firstly, do you get any error message with the M query?

In addition, do you only have the issue with 'q' search term ? What about 's' search_scope?

What's more, you could have a reference of the link below.

https://community.powerbi.com/t5/Desktop/Retrieve-API-response-headers/td-p/423704

Best  Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.