title: QNetworkRequest with headers
body: <p>I am using Qt to interact with a GraphQL host. I am using <code>QNetworkRequest</code>, and the login process is done well, gives me a <code>token</code> in JSON format. Since then, GraphQL expects the <code>token</code> to be in the HTTP header:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
     "Authorization": "Bearer &lt;token&gt;"
  }
  </code></pre>
  
  <p>Testing the server, I wrote a small Python code, which works well:</p>
  
  <pre class="lang-py prettyprint-override"><code>headers = {'Authorization': "Bearer {0}".format(token)}
  url = "http://example.com:8000/graphql"
  params = {'query': '{fieldQueries{fields(userId:"xxx"){fieldName fieldID}}}'} 
  result = requests.post(url, params=params, headers=headers)
  print(result.json())
  </code></pre>
  
  <p>The below code is supposed to do the same operation in Qt:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>QUrl url = QUrl("http://example.com:8000/graphql");
  QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
  QNetworkRequest request(url);
  QString query = QString("{fieldQueries{fields(userId:\"%1\"){fieldName fieldID}}}").arg(userId);
  QUrlQuery params;
  params.addQueryItem("query", query);
  
  connect(mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(onQueryFinish(QNetworkReply*)));
  connect(mgr, SIGNAL(finished(QNetworkReply*)), mgr, SLOT(deleteLater()));
  
  auto header = QString("Bearer %1").arg(token);
  request.setRawHeader(QByteArray("Authorization"), header.toUtf8());
  
  mgr-&gt;post(request, params.query().toUtf8());
  </code></pre>
  
  <p>However, server gives back an <code>internal server error (500)</code>.</p>
  
  <p>As soon as I comment out the <code>request.setRawHeader</code>, server gives back <code>You are not authorized to run this query.\nNot authenticated</code> with no error.</p>
  
  <ul>
  <li>How to make Qt to send this header correctly?</li>
  </ul>
  
  <hr>
  
  <ul>
  <li>I don't know if it helps, but I checked the packets using <em>WireShark</em>. The Python generated packet for this request is a single packet (around 750 bytes), though Qt request is broken into two packets which the length of the first is 600 byte.</li>
  </ul>
  
  <p>The working package:</p>
  
  <pre><code>...POST /graphql?query=%7BfieldQueries%7Bfields%28userId%3A%22xxx%22%29%7BfieldName+fieldID%7D%7D%7D
  HTTP/1.1..Host: xxx:8000..
  User-Agent: python-requests/2.21.0..
  Accept-Encoding: gzip, deflate..
  Accept: */*..
  Connection: keep-alive..
  Content-Type: application/json..
  Authorization: Bearer &lt;688 bytes token&gt;..Content-Length: 0....
  </code></pre>
  
  <p>Qt generated packages:</p>
  
  <pre><code>....POST /graphql HTTP/1.1..Host: xxx:8000..
  Authorization: Bearer &lt;364 bytes token&gt;..
  Content-Type: application/json..
  Content-Length: 113..
  Connection: Keep-Alive..
  Accept-Encoding: gzip, deflate..
  Accept-Language: en-US,*..
  User-Agent: Mozilla/5.0....
  </code></pre>
  
  <p>and</p>
  
  <pre><code>.e..query=%7BfieldQueries%7Bfields(useId:%22xxx%22)%7BfieldName fieldID%7D%7D%7D
  </code></pre>
  
  <ul>
  <li>I have checked other given solutions for the header, such as
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/12487620/correct-format-for-http-post-using-qnetworkrequest">Correct format for HTTP POST using QNetworkRequest</a>,</li>
  <li><a href="https://stackoverflow.com/questions/3199039/sending-http-header-info-with-qt-qnetworkaccessmanager">Sending HTTP Header Info with Qt QNetworkAccessManager</a>and read the</li>
  <li><a href="https://doc.qt.io/qt-5/qnetworkrequest.html" rel="nofollow noreferrer">QNetworkRequest Class documentation</a>.</li>
  </ul></li>
  </ul>