microsoft / devicescript

TypeScript for Tiny IoT Devices (ESP32, RP2040, ...)
https://microsoft.github.io/devicescript/
MIT License
3.24k stars 112 forks source link

HTTP request not working as expected, SSL error #607

Open varogonz95 opened 10 months ago

varogonz95 commented 10 months ago

Tip: From Visual Studio Code, you can use the command DeviceScript: Report Issue... to prefill the issue template.

Describe the bug From a ESP32 device, I want to post some data to a Cloud Firestore, thus I'm using fetch(...) function to achieve that. The store is currently unprotected for the moment, so any data can be posted without much overhead. Thing is, I get a socket tls://firestore.googleapis.com:443 error tls://firestore.googleapis.com:443: self-signed certificate. This is my code:

const url = `https://firestore.googleapis.com/v1/projects/{PROJECT_ID}/databases/(default)/documents/{COLLECTION_ID}`
const options = {
    method: "POST",
    body: JSON.stringify(...),
    headers: { "Content-Type": "application/json", key: API_KEY }
}
const response = await fetch(url, options)
//...

NOTE: I tried exactly the same with MicroPython, and works like a charm. Here is my python code:

import urequests
#...
response = urequests.post(
    f'https://firestore.googleapis.com/v1/projects/{PROJECT_ID}/databases/(default)/documents/{COLLECTION_ID}',
    json=data,
    headers={
        'Content-Type': 'application/json',
        'key': C.FIRESTORE_API_KEY
    },
)

So, how's this different from what I'm doing with MicroPython?

To Reproduce Steps to reproduce the behavior:

  1. Connect device to WIFI access
  2. Make a fetch(...) request to Cloud Firestore
  3. Get SSL Self Signed Cert error
  4. Cry

Expected behavior Be able to successfully make a request to Cloud Firestore.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Console Output:

VM> connecting to tls://firestore.googleapis.com:443
VM> socket tls://firestore.googleapis.com:443 open undefined
VM> req: POST /v1/projects/[PROJECT_ID]/databases/(default)/documents/sensors HTTP/1.1
VM> content-type: application/json
VM> key: [API_KEY]
VM> user-agent: DeviceScript fetch()
VM> accept: */*
VM> host: firestore.googleapis.com
VM> connection: close
VM> content-length: 2
VM>
VM> socket tls://firestore.googleapis.com:443 error tls://firestore.googleapis.com:443: self-signed certificate