How to set request body of the nested list of data in HTTPoison multipart?

I have an post url parameters of format &room_details: [{id: 1, passangers: [{name: "hello"}]] . How to set this data in multi_part section.

I have tried something in payload as

payload = [{"room_details", Jason.encode!([%{id: 1, passangers: [%{name: "hello"}]])}]

and sending the request body as tuple {:multipart, payload}.

Thanks.

multipart/form-data and JSON usually aren’t used together in one request; can you describe the Content-Type and payload you’re looking for?

Example Request

https://host.com/?action=hotel_reservation&roomDetails=[{"numberOfChilds":"0","roomClassId":"{{room_class_unique_id}}","passangers":[{"salutation":"Mr","first_name":"john","last_name":"ankanna"}]},{"numberOfChilds":"0","roomClassId":"{{room_class_unique_id}}","passangers":[{"salutation":"Mr","first_name":"john","last_name":"ankanna"}]}]

It’s a method POST request.
How can I set the request body here. From post man those are query params.

I found one example of an API that looks just like this for mercan.travel, on a “code examples” site that scraped a now-deleted Github repo:

https://hotexamples.com/site/file?hash=0xf7d7d85567d7cb8bc8c120b653cf684b031cdc16dbadd049a3e6a2695d28d7af&fullName=mercan_reserve.php&project=jnaroogheh/darvishi

but it uses GET for action=hotel_reservation:

$params = array();
$params['section_unique_id'] = $SectionUniqueId;
$params['action'] = 'hotel_reservation';
$params['unique_id'] = $SearchUniqueId;
$params['hotel_id'] = $HotelId;
$params['agent_ref_no'] = 4444;
$params['roomDetails'] = '[{"numberOfChilds":1,"roomClassId":"' . $ClassUniqueId . '","passangers":[{"salutation":"Mr","first_name":"' . $_POST['name'] . '","last_name":"' . $_POST['family'] . '"}]}]';
//var_dump($params);exit;
$final = json_decode(get_Requestbody($params));
print_r($final);
function get_Requestbody($param)
{
    $params = $param;
    $params['username'] = Public_Api_Username;
    $params['password'] = Public_Api_Password;
    $params['gzip'] = 'no';
    $url = Public_Api_URl;
    $str = '';
    foreach ($params as $index => $value) {
        $str .= $index . '=' . $value . '&';
    }
    //remove last '&'
    $str = trim($str, "&");
    $url .= '?' . $str;
    //var_dump($url);exit;
    $opts = array('http' => array('method' => "GET", 'header' => "Content-Type: text/html; charset=utf-8"));
    $context = stream_context_create($opts);
    $response = '';
    $c = 1;
    while (!$response && $c < 3) {
        $response = file_get_contents($url, false, $context);
        sleep($c + 3);
        $c++;
    }
    return $response;
}

I recommend you get a multipart request to your target endpoint working in Postman and post it here; that will help explain exactly what the API is expecting to see.

Hi @al2o3cr Thanks for your time and effort.

Here, I’m sharing working postman curl and postman Screenshot. I also tried encoding the url params with the help of URI.encode_query. But, that did not work either.

Curl Example

curl --location -g --request POST '{{hosst}}/ws/index.php?action=hotel_reservation&username={{usernaame}}&password={{passsword}}&unique_id=146028572&
hotel_id=12e9e81ede&
section_unique_MDAwMTYyNjo4MF80NTE0MC0tLS0tLS0tLS1TMDAwMDAxNjI2XzJfMQ==&expected_price=2144.8&agent_ref_no=GRNC&roomDetails=
[{\"numberOfChilds\":\"0\",\"passangers\":[{\"first_name\":\"ganddasdagda\",\"last_name\":\"man\",\"salutation\":\"Mr\"},{\"first_name\":\"sand\",\"last_name\":\"dad\",\"salutation\":\"Mr\"}],\"roomClassId\":\"0_0_45140_1\"}]&
gzip=no'

Let me know if you need more information.

I still don’t understand your original question - both of the requests you’ve shown are using the POST method but passing all their parameters in the URL’s query string; there’s no multipart anything happening.

I’m also very skeptical that the API expects JSON in a parameter with \ mixed in.

Do you have a link to documentation for what you’re trying to call?

Unfortunately I don’t have docs… @al2o3cr
Thanks for Everything.

Does this help?

# Define the list of data to include in the request body
data = [
  [
    {:name, "John"},
    {:age, 35},
    {:email, "john@example.com"},
  ],
  [
    {:name, "Jane"},
    {:age, 32},
    {:email, "jane@example.com"},
  ],
]

# Use HTTPoison.post!/2 to send a POST request with the data
response = HTTPoison.post!(
  "https://example.com/api/users",
  data,
  [
    # Set the content type to multipart/form-data
    {"Content-Type", "multipart/form-data"},
    # Use the :multipart option to specify that the request body should be
    # formatted as multipart/form-data
    multipart: :multipart,
  ]
)

# Handle the response
case response.status_code do
  200 ->
    # If the request was successful, process the response data...

  _ ->
    # If the request failed, handle the error...
end

Thank for your time @sergio I will try this.
As per my understanding, I need to send list of objects and list of list right.
Correct me if I’m wrong. Treating each object as keyword list right?