1

I have created a post request in aqueduct dart and it takes json as body parameter, and I need to send that request body to thirdparty api , upon getting response from third party api I need to return that response to user. I have updated the code and printed the response header and it says http 400 (bad request)

here is the code :

@override
  Controller get entryPoint {
    String dataRecieved;
    var completer = new Completer();
    var contents = new StringBuffer();
    final router = Router();
    // Prefer to use `link` instead of `linkFunction`.
    // See: https://aqueduct.io/docs/http/request_controller/
    router.route("/uploadurl").linkFunction((request) async {
      final req = await request.body.decode();

//      print( await request.body.decode());
      HttpClient client = new HttpClient();
      client.badCertificateCallback =
          ((X509Certificate cert, String host, int port) => true);
      var auth = 'Bearer ' +
          'eyJ...';

      await client
          .postUrl(Uri.parse(
              'https://<removed>/api/datalake/v3/generateDownloadObjectUrls'))
          .then((HttpClientRequest requestSend) {
        requestSend.headers
            .add("Content-Type", "application/json; charset=UTF-8");
        requestSend.headers.add("Authorization", auth);
//        requestSend.headers.contentLength = request.body.length;
        print(req);
        requestSend.write(req);
        return requestSend.close();
      }).then((HttpClientResponse response) async {
        print(await response.contentLength);
        var resStream = response.transform(Utf8Decoder());
        await for (var data in resStream) {
          print('Received data: $data');
        }
        print(await response.statusCode);


      }).catchError((e) {
        print("Request error: $e"); // The only case
      });

      print(contents);
      return Response.ok({"key": dataRecieved});
    });



return router;

}

when I make a request from the postman , I get { "key": null }

I think I am not able to send the correct request to third party API , because when I tested third party API from the postman, it was sending correct response

My pubspec.yaml file is :

name: proxydl
description: An empty Aqueduct application.
version: 0.0.1
author: stable|kernel <jobs@stablekernel.com>

environment:
  sdk: ">=2.0.0 <3.0.0"

dependencies:
  aqueduct: ^3.0.0
  http: ^0.12.0+2

dev_dependencies:
  test: ^1.0.0
  aqueduct_test: ^1.0.0

This is what I am sending from postman as post request:

{
    "paths": [
        {
            "path": "/f1/f2.log"
        }
    ]
}

This is my first POC with Dart on the server side.

focode
  • 648
  • 1
  • 16
  • 39

1 Answers1

1

Upon further investigation I found the answer:

final req = await request.body.decode(); var envalue = json.encode(req);

For now, this worked, but I feel there might be a better answer for this

focode
  • 648
  • 1
  • 16
  • 39