I am using postman to hit programmr api and the snapshot sucessfully looks like this:
Curl version: pastebin/GQDZJALj
And then I'm trying to hit the API using JQUERY using following code:
var data = "InputJSON:" + JSON.stringify({ "mode": "java_example", "apiKey": "bb6b589027a097b38e8cc47cffb8e70a", "files": [ { "name": "Test.java", "content": "class Test{\n \n public static void main(String[] arg){\n \n System.out.println(\"wwwwwww!!\"); \n }\n\n}\n\n" } ], "eval code": ""});
$.ajax({
type: 'POST',
url: "",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
data: data,
success: function (response) {
console.log(response);
return response;
}
Raw string data:
InputJSON:{"mode":"java_example","apiKey":"bb6b589027a097b38e8cc47cffb8e70a","files":[{"name":"Test.java","content":"class Test{\n \n public static void main(String[] arg){\n \n System.out.println(\"wwwwwww!!\"); \n }\n\n}\n\n"}],"eval code":""}
The APIs response keep saying that my JSon is Invalid
{error: "Invalid JSON"}
My question is how to request to the API using the Javascript/JQuery just like postman done?
Any help will be gratefully appreciated.
Thanks in advance
updated:
it should be:
var data = "InputJSON=" + JSON.stringify({ "mode": "java_example", "apiKey": "bb6b589027a097b38e8cc47cffb8e70a", "files": [ { "name": "Test.java", "content": "class Test{\n \n public static void main(String[] arg){\n \n System.out.println(\"wwwwwww!!\"); \n }\n\n}\n\n" } ], "eval code": ""});
I am using postman to hit programmr. api and the snapshot sucessfully looks like this:
Curl version: pastebin./GQDZJALj
And then I'm trying to hit the API using JQUERY using following code:
var data = "InputJSON:" + JSON.stringify({ "mode": "java_example", "apiKey": "bb6b589027a097b38e8cc47cffb8e70a", "files": [ { "name": "Test.java", "content": "class Test{\n \n public static void main(String[] arg){\n \n System.out.println(\"wwwwwww!!\"); \n }\n\n}\n\n" } ], "eval code": ""});
$.ajax({
type: 'POST',
url: "http://console.programmr./api/eval",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
data: data,
success: function (response) {
console.log(response);
return response;
}
Raw string data:
InputJSON:{"mode":"java_example","apiKey":"bb6b589027a097b38e8cc47cffb8e70a","files":[{"name":"Test.java","content":"class Test{\n \n public static void main(String[] arg){\n \n System.out.println(\"wwwwwww!!\"); \n }\n\n}\n\n"}],"eval code":""}
The APIs response keep saying that my JSon is Invalid
{error: "Invalid JSON"}
My question is how to request to the API using the Javascript/JQuery just like postman done?
Any help will be gratefully appreciated.
Thanks in advance
updated:
it should be:
var data = "InputJSON=" + JSON.stringify({ "mode": "java_example", "apiKey": "bb6b589027a097b38e8cc47cffb8e70a", "files": [ { "name": "Test.java", "content": "class Test{\n \n public static void main(String[] arg){\n \n System.out.println(\"wwwwwww!!\"); \n }\n\n}\n\n" } ], "eval code": ""});
Share
Improve this question
edited Apr 13, 2018 at 15:10
Yusuf Ibrahim
asked Apr 13, 2018 at 14:01
Yusuf IbrahimYusuf Ibrahim
1,6195 gold badges24 silver badges49 bronze badges
9
-
Is
InputJSON
supposed to be the POST parameter name here? In that case you should not do string concatenation with:
in between, but pass this as a proper object with that key, and your data. You should not even need to specify contentType and the additional header then, because those are default. – C3roe Commented Apr 13, 2018 at 14:09 - please check my postman snaphot, yes, it's should be there – Yusuf Ibrahim Commented Apr 13, 2018 at 14:10
-
Then try adding
processData: false
, so that jQuery leaves your data as-is, and doesn’t try any additional manipulation/transformation of it. – C3roe Commented Apr 13, 2018 at 14:14 - oh okay I will try, I will let you know about the result, btw this is the curl version pastebin./GQDZJALj – Yusuf Ibrahim Commented Apr 13, 2018 at 14:15
-
And why is that cURL version using
InputJSON={ "mode":
? – C3roe Commented Apr 13, 2018 at 14:18
1 Answer
Reset to default 1InputJSON
is supposed to be the POST parameter name here, so it should not be concatenated with the data with :
in between.
It should follow the normal name=value
format.