flutter dart get.http response giving html instead of json api

Viewed 1515

Yesterday the code works and returned me api json but not anymore now.

This is the url im using: https://www.instagram.com/p/CKNvnNOFPol/?__a=1

And this is what im getting from that link: Image: https://prnt.sc/xszvri

this is the code

    void getContentPost() async {
var response =  await http.get('https://www.instagram.com/p/CKNvnNOFPol/?__a=1');
print(response.body);

Map data = jsonDecode(response.body);
print(data);}

and i used elevated button to call getContentPost();

results:

Syncing files to device sdk gphone x86 arm...
Reloaded 1 of 623 libraries in 1,120ms.
I/flutter ( 2137): <!DOCTYPE html>
I/flutter ( 2137): <html lang="en" class="no-js not-logged-in client-root">
I/flutter ( 2137):     <head>
I/flutter ( 2137):         <meta charset="utf-8">
I/flutter ( 2137):         <meta http-equiv="X-UA-Compatible" content="IE=edge">
I/flutter ( 2137): 
I/flutter ( 2137):         <title>
I/flutter ( 2137): Login • Instagram
I/flutter ( 2137): </title>
I/flutter ( 2137): 
I/flutter ( 2137):         
I/flutter ( 2137):         <meta name="robots" content="noimageindex, noarchive">
I/flutter ( 2137):         <meta name="apple-mobile-web-app-status-bar-style" content="default">
I/flutter ( 2137):         <meta name="mobile-web-app-capable" content="yes">
I/flutter ( 2137):         <meta name="theme-color" content="#ffffff">
I/flutter ( 2137):         <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover">
I/flutter ( 2137):         <link rel="manifest" href="/data/manifest.json">
I/flutter ( 2137): 
I/flutter ( 2137):         <link rel="preload" href="/static/bundles/metro/ConsumerUICommons.css/963b2667869f.css" as="style" type="text/css" crossorigin="anonymous" />
I/flutter ( 2137): <link rel="preload" href="/static/bundles/metro/ConsumerAsyncCommons.css/0608bd6190e0.css" as="style" type="text/css" crossorigin="anonymous" />
I/flutter ( 2137): <link rel="preload" href="/static/bundle
E/flutter ( 2137): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 2137): <!DOCTYPE html>
E/flutter ( 2137): ^
E/flutter ( 2137): 
E/flutter ( 2137): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
E/flutter ( 2137): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1271:9)
E/flutter ( 2137): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:936:22)
E/flutter ( 2137): #3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter ( 2137): #4      JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter ( 2137): #5      JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter ( 2137): #6      jsonDecode (dart:convert/json.dart:96:10)
E/flutter ( 2137): #7      _HomeState.getContentPost (package:instagram_content_downloader_new/main.dart:41:16)
E/flutter ( 2137): <asynchronous suspension>
E/flutter ( 2137): 
1 Answers

I tried your code locally and it works without issues. Here's the snippet of the code I've tried.

Future<void> _request() async {
  var response = await http.get(Uri.parse('https://www.instagram.com/p/CKNvnNOFPol/?__a=1'));
  debugPrint('Response: ${response.body}');
}

The http request will only return an html response if ?__a=1 was removed from the endpoint url. Make sure that the endpoint that you're using is correct.

Related