将Flutter POST请求发送到Firebase Functions POST请求

时间:2020-05-23 07:15:47

标签: firebase flutter axios google-cloud-functions http-post

我在Flutter中有一个POST请求,效果很好,但CORS阻止了它。 (我已经在chrome上禁用了安全性并对其进行了正确的测试)。因此,我决定将其设置为Firebase Functions中的可调用函数,似乎可以与服务器联系,但不推送数据吗?

我使用了express,但没有给我任何回应,所以我决定使用axios,这是一个基于Promise的HTTP客户端。它记录响应,但随后超时。

很高兴有任何输入可以发布此数据。

Flutter POST(除CORS阻止以外的其他方法)

  if (softwareProvider == 'provider') {
    String url = 'https://website.com/EnquiryServlet?license=12345';
    Response response = await post(url, body: {
      'firstName': firstName,
      'lastName': lastName,
      'email': email,
    });
    print(response.body);

index.js中的云功能

const axios = require('axios');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.providerPOST = functions.https.onRequest((req,res) => {
    axios.post('https://website.com/EnquiryServlet?license=12345', {
        firstName: 'John',
        lastName: 'Smith',
        email: 'john.smith@gmail.com'
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
})

Firebase功能响应

{
 insertId: "000000-2e4eaf82-b65b-4db6-b011-79bef75df317"  

labels: {
  execution_id: "v5vcxf9ftvxb"   
 }
 logName: "projects/<project>/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
 receiveTimestamp: "2020-05-23T06:18:09.673916392Z"  

resource: {

labels: {
   function_name: "providerPOST"    
   project_id: "<ProjectName>"    
   region: "us-central1"    
  }
  type: "cloud_function"   
 }
 severity: "INFO"  
 textPayload: "{ status: 200,
  statusText: '',
  headers: 
   { 'set-cookie': 
      [ 'JSESSIONID=A8EA8FE7D88DF54D74DD4D13CB5593B6; Path=/infoware; Secure; HttpOnly' ],
     'cache-control': 'no-cache, no-store, must-revalidate',
     pragma: 'no-cache',
     expires: 'Thu, 01 Jan 1970 00:00:00 GMT',
     'transfer-encoding': 'chunked',
     date: 'Sat, 23 May 2020 06:18:00 GMT',
     connection: 'close' },
  config: 
   { url: 'https://website.com/EnquiryServlet?license=12345',
     method: 'post',
     data: '{"firstName":"John","lastName":"Smith","email":"john.smith@gmail.com"}',
     headers: 
      { Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json;charset=utf-8',
        'User-Agent': 'axios/0.19.2',
        'Content-Length': 87 },
     transformRequest: [ [Function: transformRequest] ],
     transformResponse: [ [Function: transformResponse] ],
     timeout: 0,
     adapter: [Function: httpAdapter],
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus] },
  request: 
   ClientRequest {
     domain: 
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        members: [Array] },
     _events: 
      { socket: [Function],
        abort: [Function],
        aborted: [Function],
        error: [Function],
        timeout: [Function],
        prefinish: [Function: requestOnPrefinish] },
     _eventsCount: 6,
     _maxListeners: undefined,
     output: [],
     outputEncodings: [],
     outputCallbacks: [],
     outputSize: 0,
     writable: true,
     _last: true,
     upgrading: false,
     chunkedEncoding: false,
     shouldKeepAlive: false,
     useChunkedEncodingByDefault: true,
     sendDate: false,
     _removedConnection: false,
     _removedContLen: false,
     _removedTE: false,
     _contentLength: null,
     _hasBody: true,
     _trailer: '',
     finished: true,
     _headerSent: true,
     socket: 
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: 'website.com',
        npnProtocol: undefined,
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 9,
        connecting: false,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'website.com',
        _readableState: [Object],
        readable: true,
        domain: [Object],
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        _bytesDispatched: 339,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: [Object],
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        [Symbol(asyncId)]: 62,
        [Symbol(bytesRead)]: 0 },
     connection: 
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: 'website.com',
        npnProtocol: undefined,
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 9,
        connecting: false,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'website',
        _readableState: [Object],
        readable: true,
        domain: [Object],
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        _bytesDispatched: 339,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: [Object],
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        [Symbol(asyncId)]: 62,
        [Symbol(bytesRead)]: 0 },
     _header: 'POST /infoware/servlet/EnquiryServlet?license=47088 HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/json;charset=utf-8\r\nUser-Agent: axios/0.19.2\r\nContent-Length: 87\r\nHost: website.com\r\nConnection: close\r\n\r\n',
     _onPendingData: [Function: noopPendingOutput],
     agent: 
      Agent {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 443,
        protocol: 'https:',
      "  
 timestamp: "2020-05-23T06:18:00.946Z"  
 trace: "projects/<ProjectName>/traces/b64129eb88a8645370975b0c4092f008"  
}

Firebase功能结果

providerPOST

Function execution took 60077 ms, finished with status: 'timeout'

Firebase功能结果扩展

{
 insertId: "000000-436491af-4f88-4224-b9e6-405aa0f73839"  

labels: {
  execution_id: "v5vcxf9ftvxb"   
 }
 logName: "projects/<Project Name>/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
 receiveTimestamp: "2020-05-23T06:18:09.673916392Z"  

resource: {

labels: {
   function_name: "providerPOST"    
   project_id: "<Project name>"    
   region: "us-central1"    
  }
  type: "cloud_function"   
 }
 severity: "DEBUG"  
 textPayload: "Function execution started"  
 timestamp: "2020-05-23T06:17:59.137440714Z"  
 trace: "projects/<project name>/traces/b64129eb88a8645370975b0c4092f008"  
}

0 个答案:

没有答案