Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created January 3, 2024 09:13
Show Gist options
  • Save plugin-republic/4a4a1c1a781da3d67917a881e55e6c67 to your computer and use it in GitHub Desktop.
Save plugin-republic/4a4a1c1a781da3d67917a881e55e6c67 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Product Add-Ons Ultimate REST API Tester</title>
</head>
<body>
<script src='wp-includes/js/jquery/jquery.min.js'></script>
<p>API Key: <input type="text" id="wc_api_key" value="" size="50"></p>
<p>API Secret: <input type="text" id="wc_api_secret" value="" size="50"></p>
<p>API Method:
<select id="wc_api_method">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</p>
<p>API URL: <input type="text" id="wc_api_url" size="70"><br>
e.g.<br>
https://[domain]/wp-json/wc/pewc/[product_id]/ Accepts: GET (returns all group IDs), POST (create group)<br>
https://[domain]/wp-json/wc/pewc/[product_id]/[group_id]/ Accepts: GET (returns group data and all field IDs), POST (create field), PUT (update group data), DELETE<br>
https://[domain]/wp-json/wc/pewc/[product_id]/[group_id]/[field_id] Accepts: GET (return field data), PUT (update field), DELETE
</p>
<p>JSON Request:</p>
<textarea id="wc_api_json_request" rows="10" cols="100"></textarea>
<p>JSON Response:</p>
<textarea id="wc_api_json_response" rows="10" cols="100"></textarea>
<p><input type="button" id="wc_api_submit" value="Submit Request" onclick="submitRequest()"></p>
<script type="text/javascript">
function toggleRequestBox() {
if ( 'GET' == jQuery('#wc_api_method').val() || 'DELETE' == jQuery('#wc_api_method').val() ) {
jQuery("#wc_api_json_request").hide();
} else {
jQuery("#wc_api_json_request").show();
}
}
function basicAuth(key, secret) {
let hash = btoa(key + ':' + secret);
return "Basic " + hash;
}
function submitRequest() {
jQuery('#wc_api_json_response').val('');
jQuery('#wc_api_submit').prop('disabled', true);
let auth = basicAuth( jQuery("#wc_api_key").val(), jQuery("#wc_api_secret").val() );
jQuery.ajax({
url: jQuery('#wc_api_url').val(),
method: jQuery('#wc_api_method').val(),
contentType: 'application/json',
dataType: 'json',
//data: '{'+ JSON.parse( jQuery('#wc_api_json_request').val() ) + '}',
data: jQuery('#wc_api_json_request').val(),
beforeSend: function (req) {
req.setRequestHeader('Authorization', auth);
},
error: function (xhr, ajaxOptions, thrownError) {
let jsonresponse = xhr.responseJSON;
//console.log(xhr);
jQuery('#wc_api_json_response').val( xhr.status + ': ' + thrownError + '; ' + jsonresponse.code + ': ' + jsonresponse.message );
jQuery('#wc_api_submit').prop('disabled', false);
}
})
.done(function( data ){
jQuery('#wc_api_json_response').val( JSON.stringify( data ) );
jQuery('#wc_api_submit').prop('disabled', false);
})
}
// always clear on first load
jQuery('#wc_api_json_response').val('');
toggleRequestBox();
jQuery('#wc_api_method').on('change', function(){
toggleRequestBox();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment