Sending it as a query string argument (aka in the url) is deprecated like Kraken/v5 and is generally speaking bad practice (for when you move on to oAuth’ed requests)
Add authorization headers
The http
package provides a convenient way to add headers to your requests. Alternatively, use the HttpHeadersclass from the dart:io
library.
content_copy
Future<http.Response> fetchPost() {
return http.get(
'https://jsonplaceholder.typicode.com/posts/1',
// Send authorization headers to the backend.
headers: {HttpHeaders.authorizationHeader: "Basic your_api_token_here"},
);
}
class UserAgentClient extends http.BaseClient {
final String userAgent;
final http.Client _inner;
UserAgentClient(this.userAgent, this._inner);
Future<StreamedResponse> send(BaseRequest request) {
request.headers['user-agent'] = userAgent;
return _inner.send(request);
}
}
import 'dart:html';
import 'dart:convert';
void main() {
var data = { 'title' : 'My first post' };
HttpRequest.request(
'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
sendData: json.encode(data),
requestHeaders: {
'Content-Type': 'application/json; charset=UTF-8'
}
)
.then((resp) {
print(resp.responseUrl);
print(resp.responseText);
});
}
Seem to be relevant (google hits 1 and 2 and 3 for how to send a header)
No idea if any of these work, this is not a Dart Support forum.