MrJoshFisher MrJoshFisher - 1 year ago 95
JSON Question

How do I use a forward slash in my api call?

Hello all Im starting to use APIs for development purposes and ive come accross and api call that uses / as a means of separating values I've been putting ?

var requestURL = '';

$.getJSON(requestURL, {
'street' : street,
'town' : town,
'postcode' : postcode,
}, function(data) {

the result of the requesturl would be

but I've noticed that some apis have / instead of ? in the request URL

So how would I do this

var requestURL = '{{STATIONNAME}}/{{STATION DATE}}/{{STATION TIME}}/timetable.json?app_id=APPID&app_key=APIKEY&train_status=passenger';

$.getJSON(requestURL, {
'stationname' : stationname,
'date' : date,
'time' : time,
}, function(data) {

but wouldn't that put ? in-between each parameter?

Tim Tim
Answer Source

Use Percent encoding on the station name to change the / into %2F. Full details here:

In javascript this can be achieved using encodeURIComponent(str); eg

// Build request URL (assumes date and time are strings in the
// correct format)
var requestURL = 
    '' + 
    encodeURIComponent(stationname) + '/' + date + '/' + 
    time + '/timetable.json';

$.getJSON(requestURL, {
    'app_id': app_id,
    'app_key': app_key,
    'train_status': 'passenger',
}, function(data) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download