Search code examples
jqueryhttp-headerstokennoaa

Using NOAA's V2 API


I understand C# and VB, but am a bit new to web development. I am trying to write a VB web app to pull data using NOAA's new (V2) API. Their site says to first request a token (done). Then to add it to the header and use a base URL (http://www.ncdc.noaa.gov/cdo-web/api/v2/) and append the appropriate endpoint after v2/...

It then gives me:

Header
token

Usage
curl -H "token:<token>" url
OR
$.ajax({ url:<url>, data:{<data>}, headers:{ token:<token> } })
Where <token> is the token obtained from the token request page. 

My question is:

  1. How do they want me to add the token to the header? (I've tried a meta tag, but not sure if I added it properly) and
  2. How do I use that jQuery ($.ajax) to pull the data I want?

Solution

  • The header they want you to add is not a header in the HTML (i.e. not <head><meta token=token>). The header they want is an HTTP header in the request (that's what the -H option to curl does).

    For JQuery, you pass a header in the options hash:

    $.ajax({ url: 'foo/bar', headers: { 'token': 'token value' } });

    Which is what they are showing you under the curl command in the text you pasted: $.ajax({ url:<url>, data:{<data>}, headers:{ token:<token> } })

    See more about adding headers to $.ajax here