0

I am creating a custom web page, which contains a list of check boxes for a user to select the language(s) they want for translating text. The problem I am having is, how can I store the language(s) that the user has chosen and pass it as an input in my Azure Logic App query?

Peter
  • 71
  • 1
  • 10
  • how would you like to pass it in querystring? separated by comma, array, anything is fine...? – Diogo Lessa Apr 16 '20 at 00:18
  • 1
    You might have to submit several requests for each language, looking at the API. – Shivashriganesh Mahato Apr 16 '20 at 00:24
  • You can save the selection in `localstorage` or `indexdb` before submitting. Also, as @Mahato points out, you need multiple requests each for one language the user selected. – Abrar Hossain Apr 16 '20 at 00:40
  • @DiogoLessa like sending JSON to logic apps. Get HTML form to send JSON body, Something like this: {"language":"english"} – Peter Apr 16 '20 at 00:45
  • @ShivashriganeshMahato I mean can we do it without the use of the API? – Peter Apr 16 '20 at 00:45
  • Does this answer your question? [Blocked: In Azure Logic app, how can you add group of checkboxes for each of the languages for a user to choose for caption translation option?](https://stackoverflow.com/questions/61181707/blocked-in-azure-logic-app-how-can-you-add-group-of-checkboxes-for-each-of-the) – Nicolas R Apr 17 '20 at 15:04

1 Answers1

1

It might depend on your needs. However, a straight way of doing this is taking advantage of default HTML resources, and using the less javascript as possible:

HTML:

<form id="form-id">
  <ul>
    <li><label><input type="checkbox" name="language" value="english"> English</label></li>
    <li><label><input type="checkbox" name="language" value="spanish"> Spanish</label></li>
    <li><label><input type="checkbox" name="language" value="vietnamese"> Vietnamese</label></li>
    <li><label><input type="checkbox" name="language" value="somali"> Somali</label></li>
    <li><label><input type="checkbox" name="language" value="chinese"> Chinese</label></li>
    <li><label><input type="checkbox" name="language" value="amharic"> Amharic</label></li>
    <li><label><input type="checkbox" name="language" value="korean"> Korean</label></li>
    <li><label><input type="checkbox" name="language" value="russian"> Russian</label></li>
    <li><label><input type="checkbox" name="language" value="tagalog"> Tagalog</label></li>
    <li><label><input type="checkbox" name="language" value="arabic"> Arabic</label></li>
    <li><label><input type="checkbox" name="language" value="khmer"> Khmer</label></li>
    <li><label><input type="checkbox" name="language" value="thai"> Thai</label></li>
    <li><label><input type="checkbox" name="language" value="lao"> Lao</label></li>
    <li><label><input type="checkbox" name="language" value="japanese"> Japanese</label></li>
    <li><label><input type="checkbox" name="language" value="deutsch"> Deutsch</label></li>
  </ul>
  <button type="submit">Submit</button>
</form>

Javascript:

const form = document.getElementById('form-id')

form.addEventListener('submit', ev => {
  ev.preventDefault()

  const formData = new FormData(document.getElementById('form-id'));  
  const url = 'https://api.videoindexer.ai/location/Accounts/accountId/Videos/videoId/Index'
  fetch(url, {
    method: 'POST',
    body: formData
  })
})

If you try to send and check on you browser console, you'll see that it tries to send properly: enter image description here

Now, its only a matter of handling this on the backend side.

Also, if you need to collect the data to send in a custom way, you can use this function to do that:

const checkedOptions = []
document.querySelectorAll('input[name="language"]').forEach(item => {
  if(item.checked) {
    checkedOptions.push(item.value)
  }
})

The whole selected data will be inside this array, and you can use methods like .join() to concatenate them on your url.

EDIT:

If you need to send data with GET method, you can follow this as an example:

const form = document.getElementById('form-id')

form.addEventListener('submit', ev => {
  ev.preventDefault()

  const formData = new FormData(document.getElementById('form-id'));
  const languages = new URLSearchParams(formData).toString()
  const reTranslate = 'someValue'
  const anotherValue = 'anotherValue'
  const url = `https://api.videoindexer.ai/location/Accounts/accountId/Videos/videoId/Index?${languages}&reTranslate=${reTranslate}&anotherValue=${anotherValue}`

  console.log(url)
  // output: https://api.videoindexer.ai/location/Accounts/accountId/Videos/videoId/Index?language=khmer&language=lao&reTranslate=someValue&anotherValue=anotherValue
  fetch(url, { method: 'GET '})

})

But I feel like you need to understand a couple of things first:

  1. The difference between POST and GET;
  2. How to send data to the server (using fetch api, axios, xhr request, etc) and deal with the response from server;
  3. Discover which contract the API expects you to send

Cheers,

Diogo Lessa
  • 177
  • 5
  • Thank you Diago. I will go through this. Also, I updated my question above with the workflow of my logic app to give an idea. – Peter Apr 16 '20 at 00:50
  • I have a quick question. Why is the value of const url cut off, it's incomplete. You have it as "api.videoindexer.ai/location/Accounts/accountId/Videos/videoId/…" . I think it should be api.videoindexer.ai{location}/Accounts/{accountId}/Videos/{videoId}/Index[?language][&reTranslate][&includeStreamingUrls][&accessToken]. Am I wrong? – Peter Apr 16 '20 at 05:46
  • Well, it depends on what the API expects. If it expects a POST, the way I did is correct, and you need to fill all those data inside the body (formData). If it expects a GET, then the way you said is the proper one. I updated the example with the GET option. – Diogo Lessa Apr 16 '20 at 11:14
  • Thanks Diogo, the API is expecting a POST. – Peter Apr 16 '20 at 14:52
  • Since the logic app is HTTP triggered, the custom web page could simply make a request to that endpoint directly correct? – Peter Apr 16 '20 at 15:01
  • Yes, but this is another topic. Talking about http calls to a web server isn't exactly what you questioned above.. you wanted to know how to collect all the languages selected and pass it by url, right? This way the question will probably never ends. To trigger a http call is one thing. How the server will handle it, is another thing. There are too much topics being mixed, I guess. I'd recommend you to open a new question if any doubts remains about the http conversation. If you take a look on the answer, it is already too bloated. – Diogo Lessa Apr 16 '20 at 15:23