If you send a POST request to the API with JavaScript code similar to the following, you will receive an Internal Server Error.
Note: I use Ubuntu for OS, Apache for web server and Firefox for browser.
apitest.html
<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8"/>
<title>WolftamAlphaAPItest</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<table id="table" border="1">
<tr>th>Alternate Form</th></tr>
</table>
<script>
varbFileName=';
rbFileName='http://xxxxxxxxxxxxxxxxxx/wapi.rb';
var results;
var querymml = '<math><mrow%20class="integration-node"><mi%20class="character-node">a</mi><mi%20class="character-node">%2B</mi><mi%20class="character-node">b</mi><mi%20class="character-node">=</mi><mi%20class="character-node">2</mi><msqrt%20class="structure-node"><mrow%20class="integration-node%20 highlight">mi%20 class="character-node">a>/mi>>mi%20 class="character-node">b</mi>>>mrow>>mats>>>mrt>>mr;
$.ajax({
type: 'POST',
url —rbFileName,
async —false,
traditional —true,
dataType: "json",
data: {query:querymml},
success:function(json){
results=json;
console.log(json);
showResult(results);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(
'XMLHttpRequest:' + XMLHttpRequest.status+'\n' +
'textStatus:'+textStatus+'\n'+
'errorThrown:' + errorThrown.message
);
},
});
function showResult(result){
$("#table").append("<tr><td>"+result+"</td><);
}
</script>
</body>
</html>
wapi.rb
#!/usr/bin/ruby
require 'cgi'
require 'net/http'
require 'json'
require 'uri'
cgi = new CGI.new()
mml = cgi ['query']
input="input=#{mml}"
INPUT="#{input}"
APPID='appid=XXXXXXXXXXXXXX'
INCLUDEPODID='includepodid=Alternate form'
FORMAT='format=mathml'
uri="http://api.wolframalpha.com/v2/query?"+
"#{APPID}&#{INPUT}&#{INCLUDEPODID}&#{FORMAT}"
uri = URI.parse(uri)
req = Net::HTTP::Get.new(uri)
res=Net::HTTP.start(uri.host,uri.port){|http|
http.request(req)
}
data={result:res}
cgi.out({"type"=>"application/json", "charset"=>"UTF-8"}){
data.to_json
}
error.log
[Sun Jul 2106:25:01.796620 2019] [mpm_prefork:notice] [pid32227] AH00163: Apache/2.4.18(Ubuntu) configured --resuming normal operations
[Sun Jul 2106:25:01.796646 2019] [core:notice] [pid3227] AH00094: Command line: '/usr/sbin/apache2'
[Sun Jul 21 11:34:32.637240 2019] [cgid:error] [pid30236] (13) Permission denied: AH01241: exec of '/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/wapi.rb' failed
[Sun Jul 21 11:34:32.637484 2019] [cgid:error] [pid29278] [client 180.9.169.100:52500] End of script output before headers:wapi.rb, referer: http://xxxxxxxxxxxxxxxxxxxxxxx/wolframApi.html
Changing the line feed code from CRLF to LF resolved the Internal Server Error.
Let's learn more about this.
Thank you for the comments.
© 2024 OneMinuteCode. All rights reserved.