title: failing to get response from http.post [http: 499]
body: <p>I want to build a payment page with the use of a payment page provider. I therefore created a class which builds with a futureBuilder the needed Widgets. From an API I want to receive an URL which I want to embed with an iFrame. However, the future data is every time <code>null</code> and the snapshot has an error. But unfortunately this error is empty. From FlutterDevTools Network I could see that the http request remains pending. In Python I tried to do this request and it worked there (at the end I posted the relevant code of my script)</p>
  <p>I struggle with two things. 1) I didn't find information how to solve the problem when the error remains empty and 2) why does the http request keep pending?</p>
  <p>I tried e.g.</p>
  <pre><code>try {
        response = await http.post(uriUrl, headers: headers, body: body);
      } catch (e) {
        print(e.toString());
      }
  </code></pre>
  <p>But the error is empty. So it didn't help either.</p>
  <p>[<strong>UPDATE 21.10.21</strong>]: I reached back to the people from the API and they see an incoming request, but my side (client) is cancelling before the server is able to respond.</p>
  <p><a href="https://i.stack.imgur.com/GeCpj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GeCpj.png" alt="enter image description here" /></a></p>
  <p>What I've found is that 499 is a CLIENT CLOSED REQUEST which is
  a non-standard status code introduced by nginx for the case when a client closes the connection while nginx is processing the request.</p>
  <p>I've found <a href="https://stackoverflow.com/questions/12973304/possible-reason-for-nginx-499-error-codes">Possible reason for NGINX 499 error codes</a> and people are suggesting increasing the timeout, but honestly I am overwhelmed as I am inexperienced with more elaborate http questions.</p>
  <pre><code>class _PaymentState extends State&lt;Payment&gt; {
    late Future&lt;PaymentPage&gt; paymentPage;
  
    @override
    void initState() {
      paymentPage =
          APIManagerSaferpay().getPaymentPage(widget.price, widget.description);
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          body: FutureBuilder&lt;PaymentPage&gt;(
              future: paymentPage,
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  // the code ends up here
                  return Center(child: Text(snapshot.error.toString()));
                }
                switch (snapshot.connectionState) {
                  case ConnectionState.none:
                    break;
                  case ConnectionState.active:
                    return const Center(child: CircularProgressIndicator());
                  case ConnectionState.done:
                    //debugger();
                    String url = snapshot.data!.redirectUrl;
                    return Center(
                        child: WebView(
                      initialUrl: Uri.dataFromString(
                              '&lt;html&gt;&lt;body&gt;&lt;iframe src=&quot;$url&quot;&gt;&lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;',
                              mimeType: 'text/html')
                          .toString(),
                      javascriptMode: JavascriptMode.unrestricted,
                    ));
  </code></pre>
  <p>This is how I wrote the async function <code>getPaymentPage</code> that is invoked by the <code>initState()</code> method.</p>
  <pre><code>class APIManagerSaferpay {
    final String customerId = '******';
    final String terminalId = '********';
    String url = 'https://test.saferpay.com';
    final String authHeader = 'API_******_********:JsonApiPwd1_************';
  
  
    Future&lt;PaymentPage&gt; getPaymentPage(double total, String description) async {
      url = 'https://test.saferpay.com/api/Payment/v1/PaymentPage/Initialize';
      var bytes = utf8.encode(authHeader);
      var base64StrCred = base64.encode(bytes);
      var page = null;
  
      final queryParameters = {
        &quot;RequestHeader&quot;: {
          &quot;SpecVersion&quot;: &quot;1.24&quot;,
          &quot;CustomerId&quot;: &quot;******&quot;,
          &quot;RequestId&quot;: &quot;1234567890&quot;,
          &quot;RetryIndicator&quot;: 0
        },
        &quot;TerminalId&quot;: &quot;********&quot;,
        &quot;PaymentMethods&quot;: [&quot;Twint&quot;],
        &quot;Payment&quot;: {
          &quot;Amount&quot;: {&quot;Value&quot;: &quot;$total&quot;, &quot;CurrencyCode&quot;: &quot;CHF&quot;},
          &quot;OrderId&quot;: &quot;1234&quot;,
          &quot;Description&quot;: &quot;Lunchroom&quot;
        },
        &quot;ReturnUrls&quot;: {
          &quot;Success&quot;: &quot;https://amazon.com&quot;, // atm just arbitrary pages
          &quot;Fail&quot;: &quot;https://youtube.com&quot;
        }
      };
      final body = jsonEncode(queryParameters);
      final uriUrl = Uri.parse(url);
      final headers = {
        'Authorization': 'Basic $base64StrCred',
        'Content-Type': 'application/json; charset=utf-8',
        'Accept': 'application/json'
      };
      var response = null;
      response = await http.post(uriUrl, headers: headers, body: body);
  
      if (response.statusCode == 200) {
        // this code is never reached
        var jsonString = response.body;
        var decoded = utf8.decode(jsonString.runes.toList());
        page = PaymentPageFromJson(decoded);
      }
      if (response.statusCode &gt;= 400) {
        // this code is never reached
      }
      return page;
    }
  }
  </code></pre>
  <p>Screenshots from FlutterDevTools:</p>
  <p><a href="https://i.stack.imgur.com/GGwTh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GGwTh.png" alt="Flutter DevTools Network (1/2)" /></a></p>
  <p>The request remains empty even though I defined the request header:</p>
  <p><a href="https://i.stack.imgur.com/FFZNA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FFZNA.png" alt="interestingly, Request header is empty" /></a></p>
  <p>Screenshot shows that the request remains pending after the code broke (<code>// the code ends up here</code>)</p>
  <p><a href="https://i.stack.imgur.com/sjDVx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sjDVx.png" alt="the http request remains pending" /></a></p>
  <p><strong>Functioning Python Code</strong></p>
  <pre><code>import base64
  
  ...
  authHeader = f'{username}:{pw}'
  encodedCredentials = base64.b64encode(authHeader.encode()).decode()
  header = {&quot;Authorization&quot;: f&quot;Basic {encodedCredentials}&quot;, &quot;Content-Type&quot;: &quot;application/json; charset=utf-8&quot;, &quot;accept&quot;: &quot;application/json&quot;}
  
  queryParameters = {
      &quot;RequestHeader&quot;: {
          &quot;SpecVersion&quot;: &quot;1.24&quot;,
          &quot;CustomerId&quot;: ******,
          &quot;RequestId&quot;: &quot;ccfbac3a8ef5151ae98035f45a609741&quot;,
          &quot;RetryIndicator&quot;: 0
      },
      &quot;TerminalId&quot;: ********,
      &quot;PaymentMethods&quot;: [&quot;TWINT&quot;],
      &quot;Payment&quot;: {
          &quot;Amount&quot;: {&quot;Value&quot;: &quot;200&quot;, &quot;CurrencyCode&quot;: &quot;CHF&quot;},
          &quot;OrderId&quot;: &quot;2&quot;,
          &quot;Description&quot;: 'Lunchroom:'
      },
      &quot;ReturnUrls&quot;: {
          &quot;Success&quot;: 'https://amazon.com',
          &quot;Fail&quot;: 'https://youtube.com'
      }
  }
  
  
  response = requests.post(url=&quot;https://test.saferpay.com/api/Payment/v1/PaymentPage/Initialize&quot;, headers=header, json=queryParameters)
  </code></pre>