[英]Making a post request to node.js express api with flutter client
This is my first question, so I apologize in advanced for any noob mistakes on this platform.这是我的第一个问题,所以我提前为这个平台上的任何菜鸟错误道歉。
I am creating an API with Firebase Cloud Functions using Express.js.我正在使用 Express.js 使用 Firebase Cloud Functions 创建 API。 I am trying to get Flutter to communicate with this API in order to signup a user.
我正在尝试让 Flutter 与此 API 通信以注册用户。 When I test my API in Postman, everything works.
当我在 Postman 中测试我的 API 时,一切正常。 I send my request with raw JSON data, and the response gives me a JSON object of some of the account information that I will want to be displayed on the client.
我使用原始 JSON 数据发送我的请求,响应为我提供了一些我希望在客户端上显示的帐户信息的 JSON 对象。
I am using VSCode for all of this and debugging the Flutter code with the Web debugger.我将 VSCode 用于所有这些,并使用 Web 调试器调试 Flutter 代码。
I have looked around on here and have tried two solutions which bear their own errors.我在这里环顾四周,并尝试了两种解决方案,这些解决方案都有自己的错误。 The first approach:
第一种方法:
var response = await http.post(uri, body: {
'email': 'bob@email.com',
'username': 'BoB',
'password': 'password',
'passwordConfirm': 'password',
});
I get Error: XMLHttpRequest error.
我收到
Error: XMLHttpRequest error.
doing this.这样做。 I received
Error: Bad state: Cannot set the body fields of a Request with content-type "application/json".
我收到
Error: Bad state: Cannot set the body fields of a Request with content-type "application/json".
when I tried to set the headers in with this approach like this in the http.post function:当我尝试在 http.post 函数中使用这种方法设置标头时:
headers: <String, String>{'content-type': 'application/json',},
After some reading on this site, I came across the second approach:在本网站上阅读了一些内容后,我遇到了第二种方法:
HttpClient httpClient = new HttpClient();
var jsonMap = {
'email': 'bob@email.com',
'username': 'BoB',
'password': 'password',
'passwordConfirm': 'password',
};
HttpClientRequest request = await httpClient.postUrl(uri);
request.headers.add('content-type', 'application/json');
request.add(utf8.encode(json.encode(jsonMap)));
HttpClientResponse response = await request.close();
httpClient.close();
The hope being that the first method just deleted some necessary headers, however, this method produced Error: Unsupported operation: Platform._version
So I tried updating Flutter, but received the same error.希望第一种方法只是删除了一些必要的标题,但是,此方法产生了
Error: Unsupported operation: Platform._version
所以我尝试更新 Flutter,但收到相同的错误。
The API code looks like this: API 代码如下所示:
const functions = require("firebase-functions");
const signup = require("./handlers/signup");
const app = require("express")();
const cors = require("cors");
app.use(cors());
app.post("/signup", signup);
exports.api = functions.https.onRequest(app);
The first thing the signup handler does is log the request to console, and nothing I've tried has gotten that far.注册处理程序做的第一件事是将请求记录到控制台,而我尝试过的任何事情都没有做到这一点。
I am using localhost to serve the firebase functions and to run the flutter client.我正在使用 localhost 来提供 firebase 功能并运行 flutter 客户端。
I have also tried sending plain/text in the http.post request and having the handler parse it into an object to use with the code I already have, however, the request doesn't make it to the handler.我还尝试在 http.post 请求中发送纯文本/文本,并让处理程序将其解析为一个对象以与我已有的代码一起使用,但是,该请求并没有发送到处理程序。 I didn't save the code or the error(s) I received doing it that way.
我没有保存代码或我以这种方式收到的错误。 I failed to get this approach to work even with Postman.
即使使用 Postman,我也无法使用这种方法。
Thank you for your help.感谢您的帮助。 If you need more information, please let me know.
如果您需要更多信息,请告诉我。
Setting the content type to x-www-form-urlencoded
as shown worked worked:如图所示将内容类型设置为
x-www-form-urlencoded
有效:
var response = await http.post(
Uri.parse(
'http://localhost:5000/fadenworlddev/us-central1/api/signup'),
headers: <String, String>{
'Content-Type':
'application/x-www-form-urlencoded; charset=UTF-8',
},
body: <String, String>{
'username': 'BoB',
'email': 'Bob@email.com',
'password': 'password',
'passwordConfirm': 'password',
},
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.