SAP / openui5

OpenUI5 lets you build enterprise-ready web applications, responsive to all devices, running on almost any browser of your choice.
http://openui5.org
Apache License 2.0
2.94k stars 1.23k forks source link

JSONModel loadData() with POST puts oParameters into Payload as urlencoded #3986

Closed BenjyTec closed 6 months ago

BenjyTec commented 6 months ago

OpenUI5 version: V1.108

Browser/version (+device/version): Chrome Version 122.0.6261.70

URL (minimal example if possible): https://plnkr.co/edit/hahwVSW7pwqYlZ8K

Steps to reproduce the problem:

  1. call JSONModel.loadData() with oParameters and method = POST
  2. check the request sent using the Browser Developer Tools
  3. observe that with POST, the oParameters are put into the JSON payload, but as url-encoded string instead of JSON

What is the expected result?

I would have expected that either a) the oParameters always are attached to the target URL (as the official documentation indicates) b) the oParameters are attached to the payload as JSON (and not as url-encoded String)

So from my perspective there are three possible solutions:

  1. Update the documentation to indicate that when using POST, the oParameters will be put into the payload as urlencoded
  2. Put the oParameters into the URL when using POST reqests too (however I think this is handled by Ajax, so not in our control)
  3. When using POST, convert the oParameters into a JSON encoded string before passing them on to Ajax: oParameters = JSON.stringify(oParameters) This will prevent Ajax from converting it to url-encoding.

What happens instead?

The parameters passed with oParameters are put into the payload, but as url-encoded String.

Any other information? (attach screenshot if possible)

This is the Payload Section ("Nutzlast") in the Developer Tools when sending a POST request: image

IlianaB commented 6 months ago

Hello @BenjyTec ,

Thank you for sharing this finding. I've created an internal incident DINC0085751 for follow-up. The status of the issue will be updated here in GitHub.

Regards, Iliana

PeterBuchholz commented 6 months ago

Hi @BenjyTec ,

the JSONModel.loadData function triggers the request via jQuery.ajax with the following settings object: oParameters {url: 'https://www.google.com/', async: true, dataType: 'json', cache: true, data: {…}, …} async: true cache: true data: {username: 'MyUsername', password: 'MyPassword'} dataType: "json" error: ƒ () headers: {content-type: 'application/json'} success: ƒ () type: "POST" url: "https://www.google.com/"

According to the jQuery documentation https://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings the data property is: "Data to be sent to the server. If the HTTP method is one that cannot have an entity body, such as GET, the data is appended to the URL." So in your case the HTTP method is POST which can have an entity body and therefore the content of the data property does not get appended to the URL. jQuery.ajax also does not convert an object to JSON before putting it in the body but to a string that is URL encoded. We will update the documentation on the JSONModel.

Greetings, Peter.

PeterBuchholz commented 6 months ago

Hi @BenjyTec

"What I meant was that a JSON object passed in is converted to the notation param1=value1&param2=value2 instead of just being a normal JSON when put into the payload." Well it looks like jQuery.ajax does not support this. We cannot simply add this "behavior" now as it would fundamentally change the behavior of JSONModel.loadData for all users.

Greetings, Peter.

PeterBuchholz commented 6 months ago

Documentation updated with SAPUI5 Version 1.122