Search code examples
jqueryajaxpostmanhere-api

Trying to GET a job result works perfectly in Postman, but using AJAX in browser asks for app_code and app_id


I'm trying to use the HERE job REST API to get my job results. I tested my GET request in Postman and it worked perfectly using my apiKey without using any extra headers (except Content-Type: application/octet-stream).

Postman Request

But when I try to try the same request using AJAX with jQuery, I get a 400 response telling me I need an app_id and app_code. My Code:

    $.ajax({
        async: false,
        contentType: "application/octet-stream",
        type: 'GET',
        url: "https://batch.geocoder.ls.hereapi.com/6.2/jobs/{JOB_ID}/result?apiKey={API_KEY}",
    }).done(function (result) {
        console.log(result);
    });

The return header:

{"error":"Bad Request","error_description":"The request is missing the app_id and app_code parameters. They must both be passed as query parameters. If you do not have app_id and app_code, please obtain them through your customer representative or at http://developer.here.com/myapps."}

Using this normal link does work when putting it into the URL bar in my browser.

Using a combination of the app id on the https://developer.here.com/projects/ page under REST and the apikey as the api_code doesn't work either. This tells me that it's an invalid pair.

I have no idea what credentials I should actually use now.

Thanks for the help in advance!


Solution

  • As per the documentation you have to use the apikey for authentication. Please check this code for ajax which is working fine.

    <html>
    <button type="button" class="btn">Click me!</button>
    <p class="text">Replace me!!</p>
    <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
    $('.btn').click(function() {
    
      $('.text').text('loading . . ');
    
      $.ajax({
      type:"GET", 
     url:"https://batch.geocoder.ls.hereapi.com/6.2/jobs/CxnGVvxLZeCjJnGRD0udKgLgeQzNa5xq/result?apiKey={apikey}",
        success: function(result) {
          $('.text').text(JSON.stringify(result));
          console.log(result);
        },
    
      });
    
      });
     </script>
     </html>
    

    Hope this helps.