Frederick Posted June 6, 2022 Share Posted June 6, 2022 I have the following CURL script which responds with a JSON string. curl https://www.mydom.com/api/v2/inv \ -u somekey: \ -d description="Some Payment" How do I translate the above to JS and where to I read the JSON string? -- Frederick (UniGUI Complete - Professional Edition 1.90.0.1560) Quote Link to comment Share on other sites More sharing options...
Sherzod Posted June 6, 2022 Share Posted June 6, 2022 43 minutes ago, Frederick said: How do I translate the above to JS... https://reqbin.com/req/javascript/c-wyuctivp/convert-curl-to-javascript Quote Link to comment Share on other sites More sharing options...
Frederick Posted June 6, 2022 Author Share Posted June 6, 2022 Using the translator returns the following code:- var url = "https://www.mydom.com/api/v2/inv"; var xhr = new XMLHttpRequest(); xhr.open("POST", url); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log(xhr.status); console.log(xhr.responseText); }}; var data = "description=Some Payment"; xhr.send(data); I suppose that the above code is to be inserted within a UniSession.AddJs() call. However, where has the "-u somekey:" gone to? Quote Link to comment Share on other sites More sharing options...
Frederick Posted June 6, 2022 Author Share Posted June 6, 2022 I translated the CURL script at https://curlconverter.com/#javascript and the following was returned. fetch('https://www.mydom.com/api/v2/inv', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + btoa('somekey:') }, body: 'description=Some Payment' }); Does this look OK? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.