{"id":3157,"date":"2021-01-02T19:04:39","date_gmt":"2021-01-02T11:04:39","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=3157"},"modified":"2021-01-02T19:42:05","modified_gmt":"2021-01-02T11:42:05","slug":"http-api-displaying-downloaded-progress","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=3157","title":{"rendered":"HTTP API \u2013 displaying downloaded progress"},"content":{"rendered":"\n<p>The&nbsp;IHttpRequest&nbsp;object from the HTTP API will report HTTP download progress via a callback on a&nbsp;FHttpRequestProgressDelegate, which is accessible via&nbsp;OnRequestProgress(). The signature of the function we can attach to the&nbsp;OnRequestProgress()&nbsp;delegate is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">HandleRequestProgress( FHttpRequestPtr request, int32 <br> sentBytes, int32 receivedBytes )<\/pre>\n\n\n\n<p>The three parameters of the function you may write include the original\u00a0IHttpRequest\u00a0object, the bytes sent, and the bytes received so far. This function gets called back periodically until the\u00a0IHttpRequest\u00a0object completes, which is when the function you attach to\u00a0OnProcessRequestComplete()\u00a0when it gets called. You can use the values passed to your\u00a0HandleRequestProgress\u00a0function to find out your progress.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Getting ready<\/h1>\n\n\n\n<p>You will need an internet connection to use this recipe. We will be requesting a file from a public server. You can use a public server or your own private server for your HTTP request, if you&#8217;d like.<\/p>\n\n\n\n<p>In this recipe, we will bind a callback function to just the&nbsp;OnRequestProgress()&nbsp;delegate to display the download progress of a file from a server. Have a project ready where we can write a piece of code that will perform&nbsp;IHttpRequest,&nbsp;and a nice interface on which to display percentage progress.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">How to do it&#8230;<\/h1>\n\n\n\n<ol><li>Link to the&nbsp;HTTP&nbsp;API in your&nbsp;ProjectName.Build.cs&nbsp;file.<\/li><li>Construct an&nbsp;IHttpRequest&nbsp;object using the following code:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">TSharedRef&lt;IHttpRequest&gt; http = <br> HttpModule::Get().CreateRequest();<\/pre>\n\n\n\n<ol><li>Provide a callback function to call when the request progresses, which updates our user:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">http-&gt;OnRequestProgress().BindLambda(<br>    [this](FHttpRequestPtr request, int32 sentBytes, int32<br>    receivedBytes)<br>    -&gt; void<br>    {<br>        int32 contentLen =<br>        request-&gt;GetResponse()-&gt;GetContentLength();<br>        float percentComplete = 100.f * receivedBytes \/<br>        contentLen;<br><br>        UE_LOG(LogTemp, Warning, TEXT(\"Progress sent=%d bytes \/<br>        received=%d\/%d bytes [%.0f%%]\"), sentBytes, receivedBytes,<br>        contentLen, percentComplete);<br><br>    });<\/pre>\n\n\n\n<ol><li>Process your request with&nbsp;http-&gt;ProcessRequest().<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">How it works&#8230;<\/h1>\n\n\n\n<p>The&nbsp;OnRequestProgress()&nbsp;callback gets fired every so often with the bytes sent and bytes received HTTP progress. We will compute the total percentage of the download that is completed by calculating&nbsp;(float)receivedBytes\/totalLen, where&nbsp;totalLen&nbsp;is the HTTP response&#8217;s total length in bytes. Using the lambda function we attached to the&nbsp;OnRequestProgress()&nbsp;delegate callback, we can display the information through text.With the code in the previous&nbsp;<em>How to do it&#8230;<\/em>&nbsp;section as a base, it would be possible to create a UMG widget for a progress bar and call the&nbsp;.SetPercent()&nbsp;member function to reflect the download&#8217;s progress.\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;IHttpRequest&nbsp;object from the HTTP API wil [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,1],"tags":[26],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3157"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3157"}],"version-history":[{"count":1,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3157\/revisions"}],"predecessor-version":[{"id":3158,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3157\/revisions\/3158"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}