Home        Store        Docs        Blog

REST Api CORS Blocked

Hey. We have got the REST API working and displaying/updating information. We would like to integrate parts of the data to a web-application we are building, but trying to send a GET request only results in:

“Access to fetch at ‘http://192.168.2.2:4777/mavlink’ from origin ‘http://localhost:4200’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

Seems like this is something that must be allowed server-side.
linkhttps://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Is this something you are planning to allow? Can’t see any security issues as the web-application we are building must run on the laptop driving the ROV to work anyway.

It is the mavlink2rest executable that hosts the web-server for the api. How can I go about configuring this for a local test here?

Regards,
Mads
JM Robotics

Hi @maphstra,

You are right, I opened a PR doing that in Companion side:
You can do that (or something similar) for now.
It will expose the mavlink api at http://192.168.2.2:2770/mavlink

I also opened an issue in Mavlink2Rest project to discuss getting it done there.

Hi @maphstra,

I have update mavlink2rest to allow cors.
It’s necessary to replace mavlink2rest to test it:
1- Access the ROV terminal, check our documentation for more information.
2- Run this command:
screen -X -S mavlink2rest quit | sudo pkill -9 -f mavlink2rest | wget https://github.com/patrickelectric/mavlink2rest/releases/download/0.7.2/mavlink2rest-armv7-unknown-linux-musleabihf -O ~/companion/tools/mavlink2rest | chmod +x ~/companion/tools/mavlink2rest

These commands will update mavlink2rest to the last version 0.7.2, that has CORS enabled.
After finished, remember to restart companion via the web interface.

It works! That’s great guys, thanks!

Mads

Follow-up question: Using mavlink-inspector i can see SERVO_OUTPUT_RAW.servo9_raw. This value (and the higher ones) are missing from the REST-api. Anything I can do to remedy this?

We are interested in reading the camera tilt angle.

Hi Mands,

You should be able to see the lights and all the servo outputs, I just did a quick test here and it’s working:
chan9_raw and and chan10_raw are light1 and light2 with max bright.

Json
{
  "chan10_raw": 1900,
  "chan11_raw": 1100,
  "chan12_raw": 0,
  "chan13_raw": 0,
  "chan14_raw": 0,
  "chan15_raw": 0,
  "chan16_raw": 0,
  "chan17_raw": 0,
  "chan18_raw": 0,
  "chan1_raw": 1500,
  "chan2_raw": 1500,
  "chan3_raw": 1500,
  "chan4_raw": 1500,
  "chan5_raw": 1500,
  "chan6_raw": 1500,
  "chan7_raw": 1500,
  "chan8_raw": 1500,
  "chan9_raw": 1900,
  "chancount": 0,
  "message_information": {
    "counter": 543702,
    "frequency": 9.99305248260498,
    "time": {
      "first_message": "2020-06-18T22:18:36.856449734+00:00",
      "last_message": "2020-06-19T13:25:25.479091361+00:00"
    }
  },
  "rssi": 0,
  "time_boot_ms": 54414402,
  "type": "RC_CHANNELS"
}

For the camera position appears in SERVO_OUTPUT_RAW, but there is a bug in mavlink2rest where only the first 8 channels appears, you can check the status of the issue here:

It’s also possible to fetch position information from MOUNT_STATUS, but there is a problem related to the library used:

Can you try this binary to fix the SERVO_OUTPUT_RAW message (and others) ?
The command:
$(screen -X -S mavlink2rest quit | sudo pkill -9 -f mavlink2rest) || wget https://github.com/patrickelectric/mavlink2rest/releases/download/continuous/mavlink2rest-armv7-unknown-linux-musleabihf -O ~/companion/tools/mavlink2rest && chmod +x ~/companion/tools/mavlink2rest
Remember to restart the ROV.