Skip to content

Instantly share code, notes, and snippets.

@pedroresende
Created January 14, 2014 15:09
Show Gist options
  • Save pedroresende/8419795 to your computer and use it in GitHub Desktop.
Save pedroresende/8419795 to your computer and use it in GitHub Desktop.
REST Client
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="http://getbootstrap.com/docs-assets/ico/favicon.png">
<title>EZP-21118 - Implement HTTP CORS support</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<script type="text/javascript">
function createCORSRequest(method, accepts, url, body) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
xhr.setRequestHeader("Accept", "application/vnd.ez.api.Content+"+accepts);
xhr.setRequestHeader("Authorization", "Basic YWRtaW46cHVibGlzaA==");
xhr.send();
xhr.onreadystatechange=function()
{
document.getElementById("response").value = xhr.responseText;
alert('GOT RESPONSE');
}
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
function send(method, accepts, url, body) {
var xhr = createCORSRequest(method, accepts, url, body);
if (!xhr) {
throw new Error('CORS not supported');
}
}
</script>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">EZP-21118 - Implement HTTP CORS support - REST CLIENT</a>
</div>
</div>
</div>
<div class="container" style="margin-top: 100px;">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<form role="form">
Method:
<br>
<select id="method" name="method" class="caret">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
<option value="OPTIONS">OPTIONS</option>
<option value="HEAD">HEAD</option>
<option value="TRACE">TRACE</option>
<option value="CONNECT">CONNECT</option>
</select>
<br>
Accepts:
<br>
<select id="accepts" name="accepts" class="caret">
<option value="json">json</option>
<option value="xml" selected>xml</option>
</select>
<br>
URL
<input type="text" name="url" id="url" class="form-control" value="http://ezp/api/ezp/v2/content/locations/1/2">
<br>
BODY
<textarea name="body" id="body" class="form-control"></textarea>
<br>
<input type="button" value="SEND" class="btn btn-default" onclick="send(method.value, accepts.value, url.value, body.value);"/>
<br>
<br>
RESPONSE
<textarea type="text" name="response" id="response" class="form-control"></textarea>
</form>
</div>
<div class="col-md-4"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment