jaredhendrickson13 / pfsense-api

The missing REST API package for pfSense
https://pfrest.org/
Apache License 2.0
653 stars 97 forks source link

"field ID is required" error for POST /api/v2/services/dhcp_server/static_mapping #490

Closed alex2ospit closed 1 week ago

alex2ospit commented 3 weeks ago

Describe the bug when send POST request to

/api/v2/services/dhcp_server/static_mapping

it return error :

{ "code": 400, "status": "bad request", "response_id": "MODEL_REQUIRES_ID", "message": "Field id is required.", "data": [] }

To Reproduce Steps to reproduce the behavior:

send post request to /api/v2/services/dhcp_server/static_mapping

with body as per below

{ "parent_id" : "lan", "mac": "00:50:56:82:f8:79", "ipaddr": "192.168.8.101", "hostname": "testing" }

Expected behavior return with success message

pfSense Version & Package Version:

jaredhendrickson13 commented 3 weeks ago

This seems to work fine for me on the latest release candidate:

curl -s -k -u admin:pfsense -X POST https://localhost/api/v2/services/dhcp_server/static_mapping -H "Content-Type: application/json" -d '{"parent_id" : "lan", "mac": "00:50:56:82:f8:79", "ipaddr": "192.168.1.21", "hostname": "testing"}'
{
  "code": 200,
  "status": "ok",
  "response_id": "SUCCESS",
  "message": "",
  "data": {
    "parent_id": "lan",
    "id": 0,
    "mac": "00:50:56:82:f8:79",
    "ipaddr": "192.168.1.21",
    "cid": null,
    "hostname": "testing",
    "domain": "",
    "domainsearchlist": [],
    "defaultleasetime": 7200,
    "maxleasetime": 86400,
    "gateway": "",
    "dnsserver": [],
    "winsserver": [],
    "ntpserver": [],
    "arp_table_static_entry": false,
    "descr": ""
  }
}

There are a few things you can try:

  1. Try running /etc/rc.restart_webgui just to make sure the web server changes were applied correctly when the package was installed, then rerun your request.
  2. If you get the same result, double check that your request is actually being sent as a POST request. GET, PATCH and DELETE requests for this endpoint require the id field.
  3. It's possible you are somehow hitting a redirect, when pfSense redirect's it does not retain the original request method and will default to a GET request. Try submitting the request to alternates of the endpoint and see if there's any change:
    • /api/v2/services/dhcp_server/static_mapping
    • /api/v2/services/dhcp_server/static_mapping/
    • /api/v2/services/dhcp_server/static_mapping/index.php
alex2ospit commented 3 weeks ago

the request is sent to an virtual ip. i believe then the request shall be the redirect let me test and confirm with you on this.

On Sat, 29 Jun 2024 at 11:49 PM, Jared Hendrickson @.***> wrote:

This seems to work fine for me on the latest release candidate:

curl -s -k -u admin:pfsense -X POST https://localhost/api/v2/services/dhcp_server/static_mapping -H "Content-Type: application/json" -d '{"parent_id" : "lan", "mac": "00:50:56:82:f8:79", "ipaddr": "192.168.1.21", "hostname": "testing"}'

{ "code": 200, "status": "ok", "response_id": "SUCCESS", "message": "", "data": { "parent_id": "lan", "id": 0, "mac": "00:50:56:82:f8:79", "ipaddr": "192.168.1.21", "cid": null, "hostname": "testing", "domain": "", "domainsearchlist": [], "defaultleasetime": 7200, "maxleasetime": 86400, "gateway": "", "dnsserver": [], "winsserver": [], "ntpserver": [], "arp_table_static_entry": false, "descr": "" } }

There are a few things you can try:

  1. Try running /etc/rc.restart_webgui just to make sure the web server changes were applied correctly when the package was installed, then rerun your request.
  2. If you get the same result, double check that your request is actually being sent as a POST request. GET, PATCH and DELETE requests for this endpoint require the id field.
  3. It's possible you are somehow hitting a redirect, when pfSense redirect's it does not retain the original request method and will default to a GET request. Try submitting the request to alternates of the endpoint and see if there's any change:
    • /api/v2/services/dhcp_server/static_mapping
    • /api/v2/services/dhcp_server/static_mapping/
    • /api/v2/services/dhcp_server/static_mapping/index.php

— Reply to this email directly, view it on GitHub https://github.com/jaredhendrickson13/pfsense-api/issues/490#issuecomment-2198241655, or unsubscribe https://github.com/notifications/unsubscribe-auth/AYY2IXFX5WTML4PC6FLIAMLZJ3JQBAVCNFSM6AAAAABKDAUDJGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJYGI2DCNRVGU . You are receiving this because you authored the thread.Message ID: @.***>

alex2ospit commented 3 weeks ago

the request is sent to an virtual ip. i believe then the request shall be the redirect let me test and confirm with you on this. On Sat, 29 Jun 2024 at 11:49 PM, Jared Hendrickson @.> wrote: This seems to work fine for me on the latest release candidate: curl -s -k -u admin:pfsense -X POST https://localhost/api/v2/services/dhcp_server/static_mapping -H "Content-Type: application/json" -d '{"parent_id" : "lan", "mac": "00:50:56:82:f8:79", "ipaddr": "192.168.1.21", "hostname": "testing"}' { "code": 200, "status": "ok", "response_id": "SUCCESS", "message": "", "data": { "parent_id": "lan", "id": 0, "mac": "00:50:56:82:f8:79", "ipaddr": "192.168.1.21", "cid": null, "hostname": "testing", "domain": "", "domainsearchlist": [], "defaultleasetime": 7200, "maxleasetime": 86400, "gateway": "", "dnsserver": [], "winsserver": [], "ntpserver": [], "arp_table_static_entry": false, "descr": "" } } There are a few things you can try: 1. Try running /etc/rc.restart_webgui just to make sure the web server changes were applied correctly when the package was installed, then rerun your request. 2. If you get the same result, double check that your request is actually being sent as a POST request. GET, PATCH and DELETE requests for this endpoint require the id field. 3. It's possible you are somehow hitting a redirect, when pfSense redirect's it does not retain the original request method and will default to a GET request. Try submitting the request to alternates of the endpoint and see if there's any change: - /api/v2/services/dhcp_server/static_mapping - /api/v2/services/dhcp_server/static_mapping/ - /api/v2/services/dhcp_server/static_mapping/index.php — Reply to this email directly, view it on GitHub <#490 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AYY2IXFX5WTML4PC6FLIAMLZJ3JQBAVCNFSM6AAAAABKDAUDJGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJYGI2DCNRVGU . You are receiving this because you authored the thread.Message ID: @.>

No good. The REST API call is from LAN which having LAGG configuring from accessing the REST API

Internal Virtual IP --> FIrewall LAN IP --> LAGG --> REST API.

The same problem is persist.

jaredhendrickson13 commented 3 weeks ago

Hmm I've tried replicating that setup but the request is still working correctly for me. Let's double check I'm attempting to replicate this correctly. Here's what I'm doing to try to replicate the issue:

  1. I create a LAGG interface that includes my LAN interface.
  2. I give the LAGG interface a static IP configuration (192.168.8.1/24).
  3. I create an IP Alias Type virtual IP on the LAGG interface (192.168.8.10/24).
  4. I submit the following API request to configure the DHCP static mapping:
    curl -s -k -u admin:pfsense -X POST https://192.168.8.10/api/v2/services/dhcp_server/static_mapping -H "Content-Type: application/json" -d '{"parent_id" : "lan", "mac": "00:50:56:82:f8:79", "ipaddr": "192.168.8.101", "hostname": "testing"}'

Which responds with:

{
  "code": 200,
  "status": "ok",
  "response_id": "SUCCESS",
  "message": "",
  "data": {
    "parent_id": "lan",
    "id": 0,
    "mac": "00:50:56:82:f8:79",
    "ipaddr": "192.168.8.101",
    "cid": null,
    "hostname": "testing",
    "domain": "",
    "domainsearchlist": [],
    "defaultleasetime": 7200,
    "maxleasetime": 86400,
    "gateway": "",
    "dnsserver": [],
    "winsserver": [],
    "ntpserver": [],
    "arp_table_static_entry": false,
    "descr": ""
  }
}

Anything I'm doing differently?

alex2ospit commented 2 weeks ago

The virtual IP type that i am using here is "CARP" . see if you can regenerate the error at your end.

thank you

jaredhendrickson13 commented 2 weeks ago

I'm still getting a successful response even with a CARP virtual IP on the LAGG. Do still get the same error if you make the request from the pfSense command line using curl like the command I ran in my previous comment?

alex2ospit commented 2 weeks ago

i got a success by using CURL at pfsense command line.

alex2ospit commented 1 week ago

hey a little of update upon some in deep fact finding.

The request is posted to http instead of https.

Pfsense originally do redirect from http to https. thus , all the non-get method become get method.

By sending the request to https. the issue resolve automatically.

jaredhendrickson13 commented 1 week ago

Glad to hear you figured it out. I'll close this issue, but feel free to re-open an issue if you run into any more trouble.

Thanks!