gpt4 book ai didi

flutter - 如何通过 Flutter 发布到谷歌表单。响应总是 400(POSTMAN 的结果是 200 OK)

转载 作者:行者123 更新时间:2023-12-03 03:30:54 24 4
gpt4 key购买 nike

我试图用flutter POST到谷歌表单,但响应总是400。
当我使用 POSTMAN 时,它成功了。
我不明白为什么它会因 flutter 而失败。
我尝试了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
    flutter 医生的结果是
    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)

    最佳答案

    使用表单数据在 Postman 中重现您的案例后:
    postman
    为了控制,我使用 curl 进行测试:

    curl --location --request POST 'https://docs.google.com/forms/u/0/d/e/1FAIpQLSc5aAwiiLJ2qJhF-ngGFfRl8m8yEJs8gTipY7pnNQkxGcJ_1w/formResponse' \
    --form 'entry.844954850=bug' \
    --form 'entry.479013015=dsfa'
    现在我可以在 flutter 中重现这个 Post 请求:
    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(...) //------------> Before
    Map<String, String> requestBody = <String, String>{ // After
    'entry.844954850': 'bug',
    'entry.479013015': 'dsfa'
    };

    http.Response resp =
    await http.post(url, headers: headers, body: requestBody);
    print(resp);
    if (resp.statusCode != 200) {
    print('Failed to post ${resp.statusCode}');
    } else {
    print(resp.body);
    }
    }
    Debug模式下的状态码:
    result
    对于 dio,它与替换 json 相同,并且您的请求格式正确。

    关于flutter - 如何通过 Flutter 发布到谷歌表单。响应总是 400(POSTMAN 的结果是 200 OK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63971265/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com