cyberpanel icon indicating copy to clipboard operation
cyberpanel copied to clipboard

[BUG] cloudAPI does not return valid JSON

Open xmok opened this issue 10 months ago • 1 comments

When consuming cloudAPI endpoints, the expected response is valid JSON but CyberPanel is using an old method of returning responses using HttpResponse and json.dumps(...) which results in the actual response being of format text/html instead of application/json.

PROBLEM This makes consuming the API cumbersome as data must be parsed into valid JSON in JavaScript using JSON.parse(...). This can lead to further issues as some of the responses must be parsed then parsed again to access the data properly which is also not desirable.

To Reproduce

  1. Consume any cloudAPI endpoint
  2. Observe that response is of type text/html instead of application/json

Expected behavior application/json returned.

Screenshots (can also be viewed in the official CyberPanel API Documentation video @ https://youtu.be/HPCTDdEJ_gk?t=348)

CURRENT RESPONSE AS text/html cyberpanel-cloudapi-response-as-text

DESIRED RESPONSE AS application/json cyberpanel-cloudapi-response-as-json

Operating system: Irrelevant.

CyberPanel version: Issue is present up to latest CyberPanel version i.e. CyberPanel Version 2.3 Build 4

Additional context QUESTION: Is this behaviour intentional or can it be fixed? SOLUTION: replace HttpResponse with JsonResponse and modify some views and JS accordingly.

I have prepared a Proof of Concept in this branch: https://github.com/xmok/cyberpanel/tree/return-json-in-cloudAPI EDIT: Link to comparison: https://github.com/usmannasir/cyberpanel/compare/v2.3.5-dev...xmok:cyberpanel:return-json-in-cloudAPI

If it looks desirable, I can turn it into a PR and work on migrating all the other endpoints as well. It will take time but can be accomplished.

Best!

xmok avatar Oct 30 '23 16:10 xmok

@usmannasir, don't mean to bother you but could you review the above issue? I'll be willing to submit PRs if required but can't do it unless I have your go-ahead. Essentially, we'll replace HttpResponse with JsonResponse and slightly modify the code to account for the JSON file types. If required, we could also version the controller but isn't needed. Thanks!

xmok avatar Nov 23 '23 23:11 xmok