简体   繁体   English

Excel power query gives DataSource.Error web.content failed to get content from api url (404):Not Found

[英]Excel power query gives DataSource.Error web.content failed to get content from api url (404):Not Found

My excel uses data from an api url, I run the query every 10 mins, but if the api has a lot of traffic and returns a 404 error, I get a pop up error "DataSource.Error web.content failed to get content from api url (404);Not Found" and the whole spread sheet isn't working until I click okay, what I need to do is to let the query to keep trying the api url until it gets data to return instead of throwing an error. My excel uses data from an api url, I run the query every 10 mins, but if the api has a lot of traffic and returns a 404 error, I get a pop up error "DataSource.Error web.content failed to get content from api url (404);Not Found" and the whole spread sheet isn't working until I click okay, what I need to do is to let the query to keep trying the api url until it gets data to return instead of throwing an error . here's the query that I run:这是我运行的查询:

let
    Source = Json.Document(Web.Contents("api url")),
    #"Converted to Table" = Record.ToTable(Source),
    #"Removed Top Rows" = Table.Skip(#"Converted to Table",9),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Top Rows",6)
in
    #"Removed Bottom Rows"

I tried to add MaunalStatusHandling but it didn't solve the problem as I kept getting error messages.我试图添加 MaunalStatusHandling 但它并没有解决问题,因为我不断收到错误消息。 I hope that someone can show me a way to solve this我希望有人能告诉我解决这个问题的方法

thanks in advance for your help在此先感谢您的帮助

I figured out a way to solve the problem, I'll post it in case someone else has a similar problem我想出了解决问题的方法,我会发布它以防其他人遇到类似问题

let
    api_fnName = let
    Value.WaitFor = (producer as function, interval as function, optional count as number) as any =>
        let
            list = List.Generate(
                () => {0, null},
                (state) => state{0} <> null and (count = null or state{0} < count),
                (state) => if state{1} <> null
                    then {null, state{1}}
                    else {1 + state{0}, Function.InvokeAfter(() => producer(state{0}), interval(state{0}))},
                (state) => state{1})
        in
            List.Last(list),
    Web.ContentsCustomRetry = (url as text, optional options as record) => Value.WaitFor(
        (i) =>
            let
                options2 = if options = null then [] else options,
                options3 = if i=0 then options2 else options2 & [IsRetry=true],
                result = Web.Contents(url, options3 & [ManualStatusHandling={500}]),
                buffered = Binary.Buffer(result), /* avoid risk of double request */
                status = if buffered = null then 0 else Value.Metadata(result)[Response.Status],
                actualResult = if status = 500 then null else buffered
            in
                actualResult,
        (i) => #duration(0, 0, 0, i*0.1))
in
    Json.Document(Web.ContentsCustomRetry("api url")),
    results = api_fnName[results],
    results1 = results{0},
    data = results1[data],
    #"Converted to Table" = Record.ToTable(data),
    #"Kept Range of Rows" = Table.Range(#"Converted to Table",4,1)
in
    #"Kept Range of Rows"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM