Home > Mobile >  Multiple values of a parameter in HTTP request
Multiple values of a parameter in HTTP request

Time:09-11

I am working with an external API in Laravel - where I have to make the below request.

There is a parameter product which is to be repeated for each of the product - however when the below request is sent only the last value of the product is recognized at the endpoint.

$action = Http::get('https://http.cc/api/call.xml', [
                'authid' => env('API_USER'),
                'api-key' => env('API_KEY'),
                'product' => 'Some Value',
                'product' => 'Some Value',
                'customer' => 1,
            ]);

I have also tried the following but failed:

'product[]' => 'Some Value',
'product[]' => 'Some Value',
'product' => ['Some Value', 'Some Value']

Can anyone help with the solution here.

API: https://manage.logicboxes.com/kb/answer/776 (you can refer to ns parameter)

CodePudding user response:

Thanks for the link. If you look at the ns parameter, it has a link called "Array of Strings". You can click it for an explanation. It gives an example:

Example: ?variable=1&variable=2&variable=3&variable=4

Now I can understand your problem. Laravel doesn't support multiple variables with the same name. You can however still use it, but you have to build your own query string. Like so:

// start by building a normal query string
$query = http_build_query([
                           'authid'   => env('API_USER'),
                           'api-key'  => env('API_KEY'),
                           'customer' => 1,
                          ]);
// then add your products
foreach ($products as $product) {
   $query .= '&product=' . urlencode($product);
}
// finally build the url yourself
$action = Http::get('https://http.cc/api/call.xml?' . $query);

see: http_build_query() and urlencode().

CodePudding user response:

are you sure the api allows a payload with duplication of key? Can you post a link to the endpoint's documentation?

  • Related