Jump to content

How To Translate CURL Script To JS?


Frederick

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...