Fix: Handle JSON array format for chaincode arguments in REST API#1408
Open
SurbhiAgarwal1 wants to merge 1 commit intohyperledger:mainfrom
Open
Fix: Handle JSON array format for chaincode arguments in REST API#1408SurbhiAgarwal1 wants to merge 1 commit intohyperledger:mainfrom
SurbhiAgarwal1 wants to merge 1 commit intohyperledger:mainfrom
Conversation
40523b3 to
9a72278
Compare
Signed-off-by: SurbhiAgarwal1 <agarwalsurbhi1807@gmail.com>
9a72278 to
e4e7122
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.curl): Sends repeated fieldsargs=val1&args=val2, which Go correctly parses into a slice["val1", "val2"].args=["val1", "val2"]. Go receives this as a single string["[\"val1\", \"val2\"]"], causing the chaincode to receive incorrect input and fail endorsement.Fix & Improvements
Updated
invoke.goto handle both formats. Ifargsis 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.Added a check to return a clear error message if
argsis empty, preventing silent failures.Aligned the request logic to support modern UI integration patterns (JSON array strings) without breaking existing
curlor CLI scripts.Why this matters
curlcommands continue to work exactly as before.Testing Performed
Verified the following scenarios locally:
args=val1&args=val2) -> Successargs=["val1","val2"]) -> Successargs=val1) -> Success/queryfunctions (likeGetAllAssets) 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.