我尝试使用颤振发布到谷歌表单,但响应始终为 400。当我使用 POSTMAN 时,它成功了。我不明白为什么它会因颤动而失败。
我试过http包和dio包,结果总是400,请问是什么问题?如果有解决此问题的建议,请告诉我。
我试过的代码如下。
- http 包 (http: ^0.12.2)
void _request() async {
String url =
'https://docs.google.com/forms/u/0/d/e/1FAIpQLSc5aAwiiLJ2qJhF-ngGFfRl8m8yEJs8gTipY7pnNQkxGcJ_1w/formResponse';
Map<String, String> headers = {
"Content-Type": "application/x-www-form-urlencoded",
};
String body = json.encode(
{
'entry.844954850': 'bug',
'entry.479013015': 'dsfa',
},
);
http.Response resp = await http.post(url, headers: headers, body: body);
if (resp.statusCode != 200) {
print('Failed to post ${resp.statusCode}');
} else {
print(resp.body);
}
}
结果是
I/flutter ( 5834): Failed to post 400
- Dio 包 (dio: ^3.0.10)
Future _request_dio() async {
final dio = Dio();
final url =
'https://docs.google.com/forms/u/0/d/e/1FAIpQLSc5aAwiiLJ2qJhF-ngGFfRl8m8yEJs8gTipY7pnNQkxGcJ_1w/formResponse';
String body = json.encode(
{
'entry.844954850': 'bug',
'entry.479013015': 'dsfa',
},
);
var data = await dio
.post(
url,
data: body,
options: Options(contentType: Headers.formUrlEncodedContentType),
)
.then((response) {
print(response.data);
return response.data;
}).catchError((err) {
print(err);
return null;
});
}
结果是
I/flutter ( 5834): DioError [DioErrorType.RESPONSE]: Http status error [400]
当我使用 POSTMAN 并成功时,POSTMAN 中使用了以下 HTTP 代码。
POST /forms/u/0/d/e/1FAIpQLSc5aAwiiLJ2qJhF-ngGFfRl8m8yEJs8gTipY7pnNQkxGcJ_1w/formResponse HTTP/1.1
Host: docs.google.com
Content-Type: application/x-www-form-urlencoded
Cookie: S=spreadsheet_forms=yClaqeCIC2953sdlxK8fzZb97ycQG4UFviYCNtUBFtg; NID=204=vu3hzpnGl1iqwJCJgR_bPkjmqdx4uvEQYIfxgZWm9sc6KJjoOSZDFpMrSn0AtlmqjJeFb0JkX6MJnpIHtYXgXebI7CbKJy4JI_1hOfFe8fiMjM6qEbKw4BhNjaDzHyKTk6uoU4mpG34i01A2rEbch7xCs6Z_cvwfPhNvFWH_xGI
entry.844954850=bug&entry.479013015=fsda
扑医生的结果是
C:\src\flutter\bin\flutter.bat doctor --verbose
[√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Version 10.0.18363.1082], locale ja-JP)
• Flutter version 1.20.4 at C:\src\flutter
• Framework revision fba99f6cf9 (5 days ago), 2020-09-14 15:32:52 -0700
• Engine revision d1bc06f032
• Dart version 2.9.2
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
• Android SDK at C:\Users\user\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.1
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Android Studio (version 4.0)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 48.1.2
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)