使用ODL REST API添加流时遇400错误:请求报文格式异常
Looks like you ran into a common schema mismatch issue when sending flow rules to OpenDayLight's REST API. Let's break down what's going wrong and how to fix it:
Why This Error Happens
The 400 "malformed-message" error tells us that ODL's schema parser can't recognize your request body structure. Specifically, it's expecting your <flow> element to be wrapped inside a parent container (like <flows>) that matches the Yang model it uses for flow management—right now you're sending <flow> directly as the root element, which doesn't align with the API's requirements.
Step-by-Step Fixes
1. Correct the XML Request Structure
Wrap your <flow> element inside a root <flows> tag with the proper ODL namespace. Here's how your corrected XML should look (I've filled in some missing required fields too):
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <flows xmlns="urn:opendaylight:flow:inventory"> <flow> <priority>2</priority> <flow-name>Foo</flow-name> <match> <ethernet-match> <ethernet-type> <type>2048</type> </ethernet-type> </ethernet-match> </match> <!-- Required fields you might have missed --> <table_id>0</table_id> <idle-timeout>0</idle-timeout> <hard-timeout>0</hard-timeout> <instructions> <instruction> <order>0</order> <apply-actions> <action> <order>0</order> <!-- Example action: send to LOCAL port; adjust to your needs --> <output-action> <output-node-connector>LOCAL</output-node-connector> </output-action> </action> </apply-actions> </instruction> </instructions> </flow> </flows>
2. Verify Your Request URL
Make sure your REST API URL matches the structure expected by ODL. For adding a single flow to a specific node and table, the URL should look something like this:
http://<your-odl-ip>:8181/restconf/config/opendaylight-inventory:nodes/node/<node-id>/table/0/flow/<unique-flow-id>
If you're adding multiple flows at once, adjust the URL to target the parent flows container instead of a single flow entry.
3. Double-Check Request Headers
Ensure you're sending these critical headers in your request:
Content-Type: application/xml(tells ODL you're sending XML data)Authorization: Basic YWRtaW46YWRtaW4=(base64-encoded admin/admin credentials, adjust if you changed the default)
Key Notes to Avoid Future Issues
- Don't skip required fields: ODL requires fields like
table_id,idle-timeout, andinstructionsfor valid flow rules—omitting them can trigger similar schema errors. - Namespace matters: The
xmlnsattribute in the<flows>tag is non-negotiable; it tells ODL which Yang model to use for parsing your request. - Match your ODL version: If you're using an older ODL release (like Helium) or newer ones (like Neon), the schema structure might have minor differences—cross-reference your version's documentation if you still run into issues.
内容的提问来源于stack exchange,提问作者Adib Rastegarnia

