Skip to content

Commit 3e696e4

Browse files
author
Ryan Christiani
committed
Merge pull request #7 from Rchristiani/master
Adds xmltojson conversion
2 parents 4311961 + 0995b0e commit 3e696e4

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The set up is very simple, when you make a request with `$.ajax` you might right
2020
...
2121
});
2222

23-
However does to CORS you might not be able to access the API this way. To this this proxy you have to change you request to look like this.
23+
However because of CORS you might not be able to access the API this way. If the API does not offer JSONP you can use this proxy to bypass the CORS issue. To use this proxy you have to change you request to look like this.
2424

2525
$.ajax({
2626
url: 'http://proxy.hackeryou.com',
@@ -32,8 +32,20 @@ However does to CORS you might not be able to access the API this way. To this t
3232
key: apiKey,
3333
param1: value,
3434
param2: value
35-
}
35+
},
36+
xmlToJSON: false
3637
}
3738
}).then(function(res) {
3839
...
39-
});
40+
});
41+
42+
### Options to pass
43+
44+
You pass your information via the `data` object, in there there are a bunch of options you need to pass.
45+
46+
param | type | description
47+
----- | ------ | -----------
48+
reqUrl | `string` | The URL for your endpoint.
49+
params | `object` | The options that you would normally pass to the data object
50+
xmlToJSON | `boolean` | Defaults to `false`, change to true if API returns XML
51+

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Simple proxy for students to make JSON requests",
55
"main": "server.js",
66
"dependencies": {
7-
"request": "^2.65.0"
7+
"request": "^2.65.0",
8+
"xml2json": "^0.9.0"
89
},
910
"devDependencies": {},
1011
"scripts": {

server.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
var http = require('http');
22
var queryString = require('querystring');
33
var request = require('request');
4+
var xml2json = require('xml2json');
45

56
var server = http.createServer((req,res) => {
67
res.setHeader('Access-Control-Allow-Origin', '*');
78
res.setHeader("Content-Type", "application/json");
89

910
var query = queryString.parse(req.url.substring(2));
11+
if(!query.xmlToJSON) {
12+
query.xmlToJSON = false;
13+
}
1014

1115
if(req.method === 'GET' && query.reqUrl) {
1216

@@ -36,8 +40,10 @@ var server = http.createServer((req,res) => {
3640

3741
var data = queryString.stringify(params);
3842

39-
4043
request.get(url + '?' + data ,(err,response,body) => {
44+
if(query.xmlToJSON === 'true') {
45+
body = xml2json.toJson(body);
46+
}
4147
if(response.statusCode === 200) {
4248
res.writeHead(200);
4349
res.end(body);

0 commit comments

Comments
 (0)