'Flutter HTTP POST with JSON body to specific port (same port every time; cannot change)
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:
- Sends to the same port every time (doesn't change); and
- 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.
Solution 1:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tyler2P |
