颤振飞镖get.http响应给出html而不是json api
Posted
技术标签:
【中文标题】颤振飞镖get.http响应给出html而不是json api【英文标题】:flutter dart get.http response giving html instead of json api 【发布时间】:2021-05-03 05:17:22 【问题描述】:昨天代码可以正常工作并返回给我 api json 但现在不行了。
这是我使用的网址: https://www.instagram.com/p/CKNvnNOFPol/?__a=1
这就是我从那个链接得到的: 图片:https://prnt.sc/xszvri
这是代码
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);
我使用提升按钮调用 getContentPost();
结果:
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):
【问题讨论】:
我认为您请求的api是一个HTML页面,那么它将如何返回json响应? 你用过this package吗? @SamChan 是的,我正在使用 http 包 @Mukul 我不明白你的意思 :( 不是我上面提到的 api 链接吗?有 graphql 等等等。我是新的,所以我真的不知道。我怎样才能从中获取 json 信息 html 表示你的 API 存在一些错误。但是,我尝试在我的设备中运行您的代码,但我可以正确获取数据。 【参考方案1】:我在本地尝试了您的代码,它可以正常工作。这是我尝试过的代码的 sn-p。
Future<void> _request() async
var response = await http.get(Uri.parse('https://www.instagram.com/p/CKNvnNOFPol/?__a=1'));
debugPrint('Response: $response.body');
如果?__a=1
从端点 url 中删除,http 请求将仅返回 html 响应。确保您使用的端点正确。
【讨论】:
以上是关于颤振飞镖get.http响应给出html而不是json api的主要内容,如果未能解决你的问题,请参考以下文章
错误:未找到:'dart:html' 使用带有颤振的 googleapis_auth 飞镖团队包时