Trouble connecting to Ecom Express API for AWB retrieval

Hey everyone, I’m having a hard time getting the Ecom Express API to work in my project. I can get a JSON response when I use Postman to fetch an AWB Number, but I’m stuck when trying to do the same thing in my code.

Here’s what I’ve got so far:

<form id="apiForm">
  <input type="text" name="user" value="ecomtest" />
  <input type="password" name="pass" value="Sec3tP@ss" />
  <input type="number" name="qty" value="3" />
  <input type="text" name="mode" value="prepaid" />
</form>

<script>
function fetchAWB() {
  let formInfo = new FormData(document.getElementById('apiForm'));
  fetch('https://api.ecomexpress.com/v2/get_awb', {
    method: 'POST',
    body: formInfo
  })
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));
}
</script>

I’m using Razor v3 for this project. Any ideas on what I might be doing wrong? Thanks in advance for your help!

I’ve encountered similar issues with the Ecom Express API before. The problem might be related to how you’re sending the request. Instead of using FormData, try sending the data as JSON. Here’s a modified version of your code that might work:

function fetchAWB() {
  const formData = {
    user: 'ecomtest',
    pass: 'Sec3tP@ss',
    qty: 3,
    mode: 'prepaid'
  };

  fetch('https://api.ecomexpress.com/v2/get_awb', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(formData)
  })
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));
}

Also, ensure you’re using the correct API endpoint and that your credentials are valid. If this doesn’t resolve the issue, you might want to check the API documentation for any specific headers or authentication methods required.

yo MeditatingPanda, i ran into similar probs with ecom api. try adding some headers to ur request, like this:

headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: 'Basic ’ + btoa(‘ecomtest:Sec3tP@ss’)
}

Also, double check ur api endpoint. sometimes they change without tellin us ugh. good luck mate!

Hey there MeditatingPanda! I feel your pain with API troubles. Have you tried adding any headers to your request? Sometimes these APIs can be picky about authentication.

What about CORS? Are you running into any cross-origin issues? That’s bitten me before when working with external APIs.

Just curious - why are you using Razor v3 for this? Any particular reason? I’m always interested in learning about different tech stacks.

Oh, and have you checked the API docs for any rate limiting? Sometimes that can cause weird behavior if you’re hitting it too frequently.

Let us know if you make any progress! I’m really curious to see how you solve this.