Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalar Values Wrapped as Lists in get_message_body() #28

Open
ronlo04 opened this issue Oct 7, 2024 · 1 comment
Open

Scalar Values Wrapped as Lists in get_message_body() #28

ronlo04 opened this issue Oct 7, 2024 · 1 comment

Comments

@ronlo04
Copy link

ronlo04 commented Oct 7, 2024

I am encountering an issue where scalar values in my request body are being wrapped in lists when using the get_message_body() function from the RAPIClient library. This behavior results in a 400 Bad Request error from the API endpoint, as it expects scalar values but receives lists instead.

The problem seems to stem from how get_message_body() handles the serialization of data. Even when scalar values are provided, they are being converted into lists, which causes issues with the API’s expected input format.

Steps to Reproduce:

Here's a simple demo to reproduce the issue:

# Sample data
data_batch <- list(
  list(
    id = 101,
    name = "Item A",
    quantity = 10,
    price = 5.99,
    description = "Description of Item A"
  ),
  list(
    id = 102,
    name = "Item B",
    quantity = 20,
    price = 3.49,
    description = "Description of Item B"
  )
)


#### Expected Behavior:
The values like `id`, `name`, `quantity`, and `price` should remain as scalar values in the request body. For example:

```json
[
  {
    "id": 101,
    "name": "Item A",
    "quantity": 10,
    "price": 5.99,
    "description": "Description of Item A"
  },
  {
    "id": 102,
    "name": "Item B",
    "quantity": 20,
    "price": 3.49,
    "description": "Description of Item B"
  }
]

Actual Behavior:

The scalar values are being wrapped in lists, resulting in the following incorrect request body:

[
  {
    "id": [101],
    "name": ["Item A"],
    "quantity": [10],
    "price": [5.99],
    "description": ["Description of Item A"]
  },
  {
    "id": [102],
    "name": ["Item B"],
    "quantity": [20],
    "price": [3.49],
    "description": ["Description of Item B"]
  }
]

It seems that the get_message_body() function may be handling scalar values as list. From what I understand, the auto_unbox option could be involved in this behavior, causing scalar values to be unnecessarily wrapped in lists.

Is there a way to ensure that scalar values are not wrapped in lists during the request body preparation? Cheers

@mustberuss
Copy link

Try adding auto_unbox = TRUE to your get_operations() call. The add a pet example on readme.Rmd throws a 500 for a similar reason. It worked locally when I changed it to operations <- get_operations(pet_api, auto_unbox = TRUE)

Do the operations$addPet() call inside httr::with_verbose({}) as shown for the get and delete pet to see the difference in post bodies.

BTW it probably should be opened as a separate issue but httr says it's superseded by httr2.

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

No branches or pull requests

2 participants