How to post JSON via Httpx with nested data?

Suppose I have such a request:

      let response = session.post("/some/path", 
      JSON.stringify({
          "someId": `${someId}`,
          "nestedData": `[{"key1":"value1","key2":"value2"}]`
      }));

What I expect:
Correctly formed JSON body.
What I got (in stdout logs):

{\"someId\":\"deadbeef-dead-dead-dead-beefbeefbeef\",\"nestedData\":\"[{\\\"key1\\\":\\\"value1\\\",\\\"key2\\\":value2}]\"}

As you can see there are triple backslashes
Are those even ok?
Because standard frameworks on SUT-backend (SpringBoot & com.fasterxml.jackson) are going crazy about it…
If I send post requests via JSON.stringify, but without nested data, there is no problem at all…

Hi @Crosby !

But what type you expected a nestedData to be? In other words, should it be an object or a string?

Have you tried to pass it to stringify that way (without `) if it’s an object?

JSON.stringify({
   "someId": `${someId}`,
   "nestedData": [{"key1":"value1","key2":"value2"}]
}));

Cheers

To post JSON with nested data using httpx in Python:

Import the required modules:
python
Copy code
import json
import httpx
Define the nested data:
python
Copy code
data = {
‘name’: ‘John Doe’,
‘age’: 30,
‘address’: {
‘street’: ‘123 Main St’,
‘city’: ‘New York’,
‘state’: ‘NY’
}
}
Convert the data to JSON format:
python
Copy code
json_data = json.dumps(data)
Set the headers and content type:
python
Copy code
headers = {‘Content-Type’: ‘application/json’}
Make the POST request with the JSON data:
python
Copy code
response = httpx.post, data=json_data, headers=headers)
Check the response:
python
Copy code
if response.status_code == 200:
print(‘POST request successful’)
else:
print(f’POST request failed with status code: {response.status_code}')
Remember to replace with the actual URL of the API endpoint you want to send the POST request to.

Regards,
Rachel Gomez