Skip to content

Fix: Handle JSON array format for chaincode arguments in REST API#1408

Open
SurbhiAgarwal1 wants to merge 1 commit intohyperledger:mainfrom
SurbhiAgarwal1:fix/rest-api-args-handling
Open

Fix: Handle JSON array format for chaincode arguments in REST API#1408
SurbhiAgarwal1 wants to merge 1 commit intohyperledger:mainfrom
SurbhiAgarwal1:fix/rest-api-args-handling

Conversation

@SurbhiAgarwal1
Copy link
Copy Markdown

Summary

This PR fixes a chaincode invocation failure where requests from UI clients resulted in an endorsement error:
rpc error: code = Aborted desc = failed to endorse transaction.

This was caused by a mismatch in how arguments (args) were encoded between the UI (sending a single JSON array) and the Go REST API (expecting repeated form-data fields).

##Root Cause

The Go backend was using r.Form["args"] to read arguments.

  • Working (curl): Sends repeated fields args=val1&args=val2, which Go correctly parses into a slice ["val1", "val2"].
  • Failing (UI): Often sends a single field args=["val1", "val2"]. Go receives this as a single string ["[\"val1\", \"val2\"]"], causing the chaincode to receive incorrect input and fail endorsement.

Fix & Improvements

  1. Backend Robustness (Primary Fix):
    Updated invoke.go to handle both formats. If args is received as a single string, the API now attempts to parse it as a JSON array. If parsing succeeds, it uses the extracted values; otherwise, it falls back to the original behavior.
  2. Validation:
    Added a check to return a clear error message if args is empty, preventing silent failures.
  3. UI Alignment:
    Aligned the request logic to support modern UI integration patterns (JSON array strings) without breaking existing curl or CLI scripts.

Why this matters

  • Improved DX: Developers building UIs on top of these samples won't face silent request formatting issues.
  • Backward Compatibility: Existing automation scripts and curl commands continue to work exactly as before.
  • Robustness: Better error reporting for missing arguments.

Testing Performed

Verified the following scenarios locally:

  • Scenario 1: Repeated form fields (args=val1&args=val2) -> Success
  • Scenario 2: JSON array string (args=["val1","val2"]) -> Success
  • Scenario 3: Invalid JSON/Single string fallback (args=val1) -> Success
  • Scenario 4: Empty arguments -> Correct Error Returned
  • Regression: Verified that /query functions (like GetAllAssets) are unaffected.

Note to Reviewer: This change is minimal and focused on making the REST API more flexible for various client implementations while maintaining the core logic.

@SurbhiAgarwal1 SurbhiAgarwal1 requested a review from a team as a code owner April 27, 2026 01:29
@SurbhiAgarwal1 SurbhiAgarwal1 force-pushed the fix/rest-api-args-handling branch 2 times, most recently from 40523b3 to 9a72278 Compare April 27, 2026 01:32
Signed-off-by: SurbhiAgarwal1 <agarwalsurbhi1807@gmail.com>
@SurbhiAgarwal1 SurbhiAgarwal1 force-pushed the fix/rest-api-args-handling branch from 9a72278 to e4e7122 Compare April 27, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant