草庐IT

http - 来自 http post 请求的响应在中间被切断

coder 2023-07-22 原文

我正在尝试连接到一个通过 POST 请求提供一些客户数据的网络服务,但响应在中间被切断(或者可能是触发函数没有等待响应完成)。

这是在 flutter 环境中完成的,initState() 触发请求。

我有一个用于客户内容的数据服务CustomerDataService 扩展了DataService,其中包含一些常见的内容,例如发送请求等上。

简而言之,initState() 调用 CustomerDataService.getCustomers(request),后者又调用并等待 DataService.post(endpoint, request).

Http-package: import 'package:http/http.dart' as http;

initState() 是起点:

final CustomerDataService _dataService =
  new CustomerDataServiceProvider().getCustomerDataService();

@override
void initState() {
   _getCustomers();

   super.initState();
}

void _getActors() async {
   _dataService.getCustomers(
      request: new GetCustomersRequest(
        navigations: _dataService.suggestedNavigations
      ),
   ).then((response) {
     _customersResponse = response;

     /// Set the state
     refresh();
   });
}

然后我们有 CustomerDataService:

class _CustomerDataService extends DataService implements CustomerDataService

@override
Future<GetCustomersResponse> getCustomers(
  {@required GetCustomersRequest request}) async {

  String endpoint = createEndpoint(<String>[
    App.appContext.identityInstance.rootUrl,
    _CUSTOMERS_CONTROLLER,
    _GET_CUSTOMERS_ENDPOINT
  ]);

http.Response res = await post(endpoint: endpoint, request: request.toJson());

if (res.body == null) {
    return null;
  }

  try {
    /// This prints an invalid JSON that is cut in the middle
    print(res.body);
    /// This one naturally throws an exception since res.body isn't valid.
    dynamic json = jsonDecode(res.body);

    return new GetCustomersResponse.fromJson(json);
  } catch (e) {
    print("Exception caught when trying to get customers");
    print(e);
  } 

  return null;

jsonDecode 的异常是

Bad state: No element

然后我们有 DataService:

Future<http.Response> post(
  {@required String endpoint,
     Map<String, String> header,
     Map<String, dynamic> request}) async {
   if (header == null) {
     header = _getHeader();
   } else {
     header.putIfAbsent(_AUTHORIZATION_KEY, () => _headerAuthorizationValue());
   }

   http.Response res = await http.post(Uri.parse(endpoint), headers: header);

   _validateReponse(res);

   return res;
 }

我显然做错了什么,但我看不到...

DataService.post 中的请求没有在此代码中添加正文(request 参数),这是我进一步研究后将提交的另一张票,目前的解决方法就是把服务改成不期待 body 。

我已经验证该服务的行为与 postman 的预期一致。

我希望有人能看到我的错误在哪里。

谢谢!

编辑 1:

我稍微更改了代码,以便 initState() 不使用我创建的 DataServices,而是直接使用 http-package。

http.post('http://localhost:50140/api/customer/getcustomers').then((res) {
  if(res == null) {
    print('Response is empty');
  }

  print('Status code ${res.statusCode}');

  print(res.body);
});

super.initState();

并且发生了完全相同的事情,所以我认为这至少不是数据服务造成的。

编辑 2: 在有人深入研究之前,我只想说它似乎不是来自服务、http 包或数据服务的响应。

一旦我找到Bad state: no element 异常的原因,就会更新此博客

最佳答案

好吧,我不知道该怎么做,但事实证明问题标题不正确。

是终端在文本太大时将其剪切...

dataserviceshttp package 没有错误,但在从响应主体到我的强类型模型的转换中,在模型树的深处。

Customer 模型的关联属性有一个枚举,包括服务器端和客户端。

服务使用索引序列化枚举,我用于映射的库尝试按名称获取枚举(区分大小写)。

有问题的实体

@JsonSerializable()
class Item extends Object with _$ItemSerializerMixin

自动生成的映射

json['itemType'] == null
    ? null
    : /// I could probably just remove the singleWhere and add [(int)json['itemType']] instead but that would cause some hassle when I run build_runner again.
       ItemType.values
        .singleWhere((x) => x.toString() == "ItemType.${json['itemType']}")

因此,一旦我在服务器端做了一些更改(忽略了枚举的序列化并添加了另一个返回枚举字符串值的属性,改为小写)它就开始工作了。我想进一步研究这个问题,以便我可以序列化枚举索引而不是字符串值,并以这种方式映射它,但不幸的是我现在没有时间去做。

用于自动映射的包是build_runnerjson_serializable

很高兴我找到了解决方案,很抱歉该解决方案与实际帖子完全无关。我希望这至少可以帮助某人。

关于http - 来自 http post 请求的响应在中间被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667068/

有关http - 来自 http post 请求的响应在中间被切断的更多相关文章

  1. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  2. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  3. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  4. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  5. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby-on-rails - Rails - 从命名路由中提取 HTTP 动词 - 2

    Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba

  8. ruby-on-rails - 在 Ruby on Rails 中发送响应之前如何等待多个异步操作完成? - 2

    在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.

  9. ruby-on-rails - Heroku 吃掉了我的自定义 HTTP header - 2

    我正在使用Heroku(heroku.com)来部署我的Rails应用程序,并且正在构建一个iPhone客户端来与之交互。我的目的是将手机的唯一设备标识符作为HTTPheader传递给应用程序以进行身份​​验证。当我在本地测试时,我的header通过得很好,但在Heroku上它似乎去掉了我的自定义header。我用ruby​​脚本验证:url=URI.parse('http://#{myapp}.heroku.com/')#url=URI.parse('http://localhost:3000/')req=Net::HTTP::Post.new(url.path)#boguspara

  10. ruby-on-rails - 使用 HTTP.get_response 检索 Facebook 访问 token 时出现 Rails EOF 错误 - 2

    我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token

随机推荐