0

My code uses this import from the Flutter package https://pub.dev/packages/http

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

My code itself (little bit below) attempts to make a POST to my specific endpoint. This works, however, every time the POST request is sent it uses a different PORT. How do I get a POST request of this nature to work that fulfils 2 requirements:

  1. Sends to the same port every time (doesn't change); and
  2. Sends along a JSON body

My current attempt is below. Currently, this fails because every time it sends using a different port:

Future<void> createUser() async {
var url = Uri.parse('https://MY_ENDPOINT.com/createUser');
var response =
    await http.post(url, body: {'username': username, 'email': email, 'password': password});
}

I've read plenty of docs and still cannot figure this out; can anyone help? Thanks.

Matthew Trent
  • 2,611
  • 1
  • 17
  • 30
  • You cannot specify the local end port because it's ephemeral. What motivation do you have for this requirement anyway? For HTTP POST JSON body, see: https://stackoverflow.com/questions/50278258/http-post-with-json-on-body-flutter-dart/50295533 – Richard Heap Mar 31 '22 at 21:08

1 Answers1

-1

If you have access to the server (access to modify) then you should set up port forwarding in the server. You can easily find how to set up port forwarding.

Other than that if you know the IP of your endpoint then you can just replace MY_ENDPOINT with your IP and port number as following.

Future<void> createUser() async {
var url = Uri.parse('https://111.222.33.44:8000/createUser');
var response = await http.post(url, body: {
    'username': username, 
    'email': email, 
    'password': password});
};

Replace 111.222.33.44 with your IP address

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
eNeM
  • 482
  • 4
  • 13