1

Am doing an Api call in flutter.

But the response is coming in <!DOCTYPE html> format.

Is it possible to handle this type of response?

Or is it possible to convert that response in json format?

If yes? Please share your answer. Thanks in Advance.

The following is the response:

<div class="otherhead">
<!DOCTYPE html>
<!--[if IE]><![endif]-->
<!--[if IE 8 ]><html dir="ltr" lang="en" class="ie8"><![endif]-->
<!--[if IE 9 ]><html dir="ltr" lang="en" class="ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<html dir="ltr" lang="en">
<!--<![endif]-->

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Account Login</title>
    <base href="https://getbest.co.in/" />
    <script src="catalog/view/javascript/jquery/jquery-2.1.1.min.js" type="text/javascript"></script>
    <link href="catalog/view/javascript/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen" />
    <script src="catalog/view/javascript/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="catalog/view/javascript/webiarch/product-slider-zoom/jquery.elevatezoom.js" type="text/javascript">
    </script>
    <link href="catalog/view/javascript/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <link href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700" rel="stylesheet">
    <link href="catalog/view/theme/default/stylesheet/stylesheet.css" rel="stylesheet">
    <!-- blog image zoom -->
    <script src="catalog/view/javascript/webiarch/blog/lightbox-2.6.min.js" type="text/javascript"></script>
    <link href="catalog/view/javascript/webiarch/blog/lightbox.css" rel="stylesheet" type="text/css" />
    <!-- blog image zoom -->
    <!--right to left (RTL)-->
    <!--over RTL-->
    <script src="catalog/view/javascript/webiarch/custom.js" type="text/javascript"></script>
    <link href="catalog/view/javascript/jquery/magnific/magnific-popup.css" type="text/css" rel="stylesheet"
        media="screen" />
    <link href="catalog/view/theme/default/stylesheet/webinewsletter.css" type="text/css" rel="stylesheet"
        media="screen" />
    <link href="catalog/view/javascript/jquery/webiquickview.css" type="text/css" rel="stylesheet" media="screen" />
    <script src="catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js" type="text/javascript">
    </script>
    <script src="catalog/view/javascript/jquery/webiquickview.js" type="text/javascript"></script>
    <script src="catalog/view/javascript/jquery/webinewsletter.js" type="text/javascript"></script>
    <script src="catalog/view/javascript/common.js" type="text/javascript"></script>
    <link href="https://getbest.co.in/image/catalog/cart.png" rel="icon" />
    <link href="https://getbest.co.in/index.php?route=extension/analytics/progressive_web_application/manifest"
        rel="manifest" />
    <script type="text/javascript">
        function isFacebookApp() {var ua = navigator.userAgent || navigator.vendor || window.opera; return (ua.indexOf("FBAN") > -1) || (ua.indexOf("FBAV") > -1);}if (window.location.protocol == "https:" && window.isSecureContext) {if(!isFacebookApp()){if ("serviceWorker" in navigator) {if (navigator.serviceWorker.controller) {console.log("[PWA Builder] active service worker found, no need to register");} else {navigator.serviceWorker.register("https://getbest.co.in/index.php?route=extension/analytics/progressive_web_application/service_worker").then(function (reg) {console.log("[PWA Builder] Service worker has been registered for scope: " + reg.scope);});}}} else {console.log("[PWA Builder] The current browser doesn't support service workers.");}} else {console.log("[PWA Builder] The web site is not served from a secure (HTTPS) domain.");}
    </script>
    <meta name="theme-color" content="#000000" />
    <meta name="msapplication-TileColor" content="#000000" />
    <meta name="mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="#000000" />
    <meta name="apple-mobile-web-app-title"
        content="GETBEST Restaurants range from inexpensive and informal lunching" />
    <meta name="application-name" content="GETBEST" />
    <meta name="msapplication-TileImage" content="https://getbest.co.in/image/cache/catalog/dsfds-144x144.png" />
    <link rel="apple-touch-icon" sizes="57x57" href="https://getbest.co.in/image/cache/catalog/dsfds-57x57.png" />
    <link rel="apple-touch-icon" sizes="76x76" href="https://getbest.co.in/image/cache/catalog/dsfds-76x76.png" />
    <link rel="apple-touch-icon" sizes="114x114"
        href="https://getbest.co.in/image/cache/catalog/dsfds-114x114.png" />
    <link rel="apple-touch-icon" sizes="120x120"
        href="https://getbest.co.in/image/cache/catalog/dsfds-120x120.png" />
    <link rel="apple-touch-icon" sizes="144x144"
        href="https://getbest.co.in/image/cache/catalog/dsfds-144x144.png" />
    <link rel="apple-touch-icon" sizes="152x152"
        href="https://getbest.co.in/image/cache/catalog/dsfds-152x152.png" />
    <link rel="apple-touch-icon" sizes="167x167"
        href="https://getbest.co.in/image/cache/catalog/dsfds-167x167.png" />
    <link rel="apple-touch-icon" sizes="180x180"
        href="https://getbest.co.in/image/cache/catalog/dsfds-180x180.png" />
    <meta name="msapplication-square70x70logo"
        content="https://getbest.co.in/image/cache/catalog/dsfds-70x70.png" />
    <meta name="msapplication-square150x150logo"
        content="https://getbest.co.in/image/cache/catalog/dsfds-150x150.png" />
    <meta name="msapplication-square310x310logo"
        content="https://getbest.co.in/image/cache/catalog/dsfds-310x310.png" />
    <link rel="icon" sizes="128x128" href="https://getbest.co.in/image/cache/catalog/dsfds-128x128.png" />
    <link rel="icon" sizes="192x192" href="https://getbest.co.in/image/cache/catalog/dsfds-192x192.png" />
    <script type="text/javascript" async>
        async function load_jquery_not_exist() {if(!window.jQuery) {var script = document.createElement('script');script.type = "text/javascript";script.async = true;script.src = "https://code.jquery.com/jquery-2.2.4.min.js";document.getElementsByTagName('head')[0].appendChild(script);}}function start_jquery_check() {return load_jquery_not_exist();}(async() => {await start_jquery_check();})();
    </script>
    <link href="https://getbest.co.in/index.php?route=extension/analytics/progressive_web_application/preloader"
        rel="stylesheet" media="screen" />
    <script type="text/javascript" async>
        async function async_preloader() {if(window.jQuery) {$(document).ready(function(){$(window).load(function(){if ($('#pwa-holder')) {$('#pwa-holder').fadeOut(4919, function() {$('body').removeClass('pwa-loading');});}});});}}function start_async_preloader() {return async_preloader();}(async() => {await start_async_preloader();})();
    </script>
</head>
8
  • please share your response data Commented Oct 27, 2021 at 5:39
  • If your HTML is actually XHTML - i.e. a well-formed xml docuement (and it looks like it might be) - then you should treat it like XML. In that case - use any xml to json libraries (something like pub.dev/packages/xml2json). Commented Oct 27, 2021 at 5:59
  • i treid with xml2Json, it was getting invalid xml. Is there any idea, any ways thanks for reply @Andrija Commented Oct 27, 2021 at 6:25
  • I think it might not be possible to convert Html to json. However, the Flutter Html package can be used to handle this response. Commented Oct 27, 2021 at 6:30
  • 1
    which data do you need to extract? Commented Oct 27, 2021 at 9:07

1 Answer 1

1

Am doing an Api call in flutter.

But the response is coming in <!DOCTYPE html> format.

Yes, their APi is broken. This is a login page. If you make an API call and are not authenticated, they should reply with a 401 error code, not with a human readable HTML-Page.

You can try to ask them to correct this. Maybe file a bug. But to be honest, people that are that wrong about how to build an API, probably won't see this as a mistake and refuse to do something about it.

Take this as a huge red flag in further dealings with this API provider. If they cannot get the simple things right... that's not a good projection for when there are real problems to solve.

In the meantime... well... you can catch the exception and assume every error caused by broken protocol means you have not authenticated properly. That sucks from a clean programming point of view, but it probably works.

What you don't need to do is parse this. It has no information that you would need, except for the plain fact that you had invalid credentials.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, am trying to do registration in my Flutter App. Any way thanks for the reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.