getx icon indicating copy to clipboard operation
getx copied to clipboard

[GetConnect]How can I access body data in post request?

Open LuoHuacheng opened this issue 3 years ago • 6 comments

Describe the bug I want to get the body data in post request , but I can not find the correct way to do this. I found that the post method contained the 'body' parameter but when I printed it, I got ERROR:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: Class 'Request<dynamic>' has no instance getter 'body'.
E/flutter (22264): Receiver: Instance of 'Request<dynamic>'
E/flutter (22264): Tried calling: body
E/flutter (22264): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter (22264): #1      requestInterceptor (package:flutter_app/api/interceptors/request_interceptor.dart:14:17)
E/flutter (22264): #2      GetModifier.modifyRequest (package:get/get_connect/http/src/interceptors/get_modifiers.dart:37:26)
E/flutter (22264): #3      GetHttpClient._performRequest (package:get/get_connect/http/src/http.dart:195:23)
E/flutter (22264): <asynchronous suspension>
E/flutter (22264): #4      GetHttpClient.post (package:get/get_connect/http/src/http.dart:344:22)
E/flutter (22264): <asynchronous suspension>
E/flutter (22264): #5      ApiRepository.login (package:flutter_app/api/api_repository.dart:14:17)
E/flutter (22264): <asynchronous suspension>
E/flutter (22264): #6      AuthController.login (package:flutter_app/modules/auth/auth_controller.dart:59:19)
E/flutter (22264): <asynchronous suspension>

example:

/// Here is the post function code in get connect
@override
  Future<Response<T>> post<T>(
    String? url,
    dynamic body, {
    String? contentType,
    Map<String, String>? headers,
    Map<String, dynamic>? query,
    Decoder<T>? decoder,
    Progress? uploadProgress,
  }) {
    _checkIfDisposed();
    return httpClient.post<T>(
      url,
      body: body,
      headers: headers,
      contentType: contentType,
      query: query,
      decoder: decoder,
      uploadProgress: uploadProgress,
    );
  }
/// Here is my own code
import 'dart:async';
import 'dart:convert';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get_connect/http/src/request/request.dart';

FutureOr<Request> requestInterceptor(request) async {
  // final token = StorageService.box.pull(StorageItems.accessToken);

  // request.headers['X-Requested-With'] = 'XMLHttpRequest';
  // request.headers['Authorization'] = 'Bearer $token';
  print(request.headers);
  print(request.method);
  print(request.body); // error
  EasyLoading.show(status: 'loading...');
  return request;
}

Expected behavior I hope that I can get the data in request so that I can process some of them and put them in headers

Screenshots If applicable, add screenshots to help explain your problem.

Flutter Version: 2.0.3

Getx Version: 4.1.3

LuoHuacheng avatar Apr 26 '21 09:04 LuoHuacheng

Might be the default decoder jumping in. Can you access Response::bodyString or bodyBytes ?

roipeker avatar May 25 '21 02:05 roipeker

Override the post method like this

@override
Future<Response> post(String url, body,
    {String? contentType,
    Map<String, String>? headers,
    Map<String, dynamic>? query,
    Decoder? decoder,
    Progress? uploadProgress}) {
  printInfo(info: "post: $url\nbody: $body");

  return super.post(url, body,
      contentType: contentType,
      headers: headers,
      query: query,
      decoder: decoder,
      uploadProgress: uploadProgress);
}```

inyong1 avatar Aug 12 '21 16:08 inyong1

Override the post method like this

` @OverRide Future<Response> post(String url, body, {String contentType, Map<String, String> headers, Map<String, dynamic> query, Decoder decoder, Progress uploadProgress}) { printInfo(info: "post: $url\nbody: $body");

return super.post(url, body,
    contentType: contentType,
    headers: headers,
    query: query,
    decoder: decoder,
    uploadProgress: uploadProgress);

}`

@override
Future<Response> post(String url, body,
    {String? contentType,
    Map<String, String>? headers,
    Map<String, dynamic>? query,
    Decoder? decoder,
    Progress? uploadProgress}) {
  printInfo(info: "post: $url\nbody: $body");

  return super.post(url, body,
      contentType: contentType,
      headers: headers,
      query: query,
      decoder: decoder,
      uploadProgress: uploadProgress);
}

EmanoelV avatar Oct 22 '21 01:10 EmanoelV

Override the post method like this ` @OverRide Future post(String url, body, {String contentType, Map<String, String> headers, Map<String, dynamic> query, Decoder decoder, Progress uploadProgress}) { printInfo(info: "post: $url\nbody: $body");

return super.post(url, body,
    contentType: contentType,
    headers: headers,
    query: query,
    decoder: decoder,
    uploadProgress: uploadProgress);

}`

@override
Future<Response> post(String url, body,
    {String? contentType,
    Map<String, String>? headers,
    Map<String, dynamic>? query,
    Decoder? decoder,
    Progress? uploadProgress}) {
  printInfo(info: "post: $url\nbody: $body");

  return super.post(url, body,
      contentType: contentType,
      headers: headers,
      query: query,
      decoder: decoder,
      uploadProgress: uploadProgress);
}

ok, I'll try, thank you all.

LuoHuacheng avatar Oct 22 '21 10:10 LuoHuacheng

same issue here.

@inyong1 this solution also not working, getting compile error.

Then, body byteStream cannot convert without error. This is also getting 'Bad state: Stream has already been listened to' error.

below tried method's also not getting solved my issue.

void decodeStream(Stream<List<int>> byteStream) async { try { await json.decoder .bind(utf8.decodeStream(byteStream).asStream()) .fold(StringBuffer(), (StringBuffer buffer, string) => buffer..write(string)) .then((StringBuffer buffer) => print(buffer.toString())); } catch (e) { e.printInfo(); } }

2nd try. var requestParams = await options.bodyBytes .asBroadcastStream() .transform(utf8.decoder) .transform(LineSplitter()) .toList();

saravananmnm avatar Dec 03 '21 03:12 saravananmnm

same issue

0xj0hn avatar Aug 20 '22 13:08 0xj0hn

getx version: 4.6.1 To get request body.

httpClient.addRequestModifier<void>((request) async { String body = await request.bodyBytes.bytesToString(); print(body); return request; });

bytesToString() is an extension function on Stream<List<int>> in request.dart.

madhuteja avatar Jan 21 '23 05:01 madhuteja