why Github gist API is throwing 400 error? - javascript

I'm trying to use Github Gist API to create a gist. The code is shown below:
var result = {
'a.md':'aaa',
'b.md':'bbb',
}
/*
P.S. I also tried this, but it doesn't work, either
var result = {
'a.md' :{
content: 'aaa',
},
'b.md': {
content: 'bbb',
},
}
*/
$.ajax({
type: "POST",
url: 'https://api.github.com/gists',
headers: {
accept: "application/vnd.github.v3+json",
Authorization: `token ${TOKEN}`,
},
data: {
files: result,
description: 'demo',
public: false,
},
success: (response) => {
console.log(response);
},
});
but I received an error: POST https://api.github.com/gists 400 (Bad Request).
the request is:
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"
}
And I wonder why and how to fix it.
(This is not English native speaking, so don't mind my grammar mistakes)

Related

JQuery.Ajax() not working not sure if script is loading properly

I'm trying to use an API to send emails through MailChimp. I can avoid using any sort of back end by doing this. I don't know if I am setting up my Jquery script file wrong.
$(document.ready(function () {
$('#survey').click(function() {
$.ajax({
type: “POST”,
url: “https://mandrillapp.com/api/1.0/messages/send.json”,
data: {
‘key’: ‘api Key’,
‘message’: {
‘from_email’: ‘email’,
‘to’: [
{
‘email’: ‘email’,
‘name’: ‘RECIPIENT NAME (OPTIONAL)’,
‘type’: ‘to’
}
],
‘autotext’: ‘true’,
‘subject’: ‘Class Survey’,
‘html’: ‘Here is your survey link: <a>link</a>’
}
}
}).done(function(response) {
console.log(response);
});
});
});
Here are all the errors I am receiving in VS Code
I am not sure why VS Code is highlighting all of the code. I also wanted to mention the console is giving this error even though it doesnt give much info.
Uncaught SyntaxError: Invalid or unexpected token
Thanks for the help!
it's because of the wrong double quotes you are using
use this " instead of this “
use this ' instead of ‘
$(document).ready(function() {
$('#survey').click(function() {
$.ajax({
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data: {
'key': 'api Key',
'message': {
'from_email': 'email',
'to': [
{
'email': 'email',
'name': 'RECIPIENT NAME (OPTIONAL)',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'Class Survey',
'html': 'Here is your survey link: <a>link</a>'
}
}
}).done(function(response) {
console.log(response);
});
});
});

rescuegroup/Petfinder jQuery API requests

I am currently trying to get API responses from the two following API's. All of the sample code on their website is in PHP and asks for a token and token SSH, while they only give you an API key. Very lost trying to get requests to pull. The closest I've gotten is an error that says the following:
{status: "error", message: "Unable to read your data; it might not be in json format."}
here is my JS:
jQuery.get({
url: 'https://api.rescuegroups.org/http/v2.json',
type: 'post',
contentType: 'application/json',
data: {
apikey: 'XXXXXXX',
objectType:'animals',
},
dataType: 'json',
success: function (data) {
console.info(data);
}
});
Any help is greatly appreciated. Really want to avoid having to learn PHP as I'm still very new to JS.
the problem is that you are not passing it as json.
this is json format.
let dataObject = {
data: {
apiKey: "xxxx"
}
};
let data = JSON.stringify(dataObject);
{
"data": {
"apikey": "xxx"
}
}
then pass data as your data.
After trying it with Postman the request went through.
Obviously I don't have the api key
{
"status": "error",
"message": "Error with your login credentials",
"messages": {
"generalMessages": [
{
"messageID": "1101",
"messageCriticality": "error",
"messageText": "Error with your login credentials."
}
],
"recordMessages": []
}
}

Graphql query with $http.post does not work

The request for the code below goes all ok but I don't get any data back (Just says "Error Not Found" in the "preview" tab of the request in chrome). I try the same query in GraphiQL and it gives back relevant data. I am not sure what I am missing here. Please advise, kind of stuck with this.
PlayService.prototype.getPlays = function(playID) {
//The query.
const playsQuery = `query ($playid: String) {
plays(id: $playid) {
id
items {
nodes {
id
name
}
}
}
}`;
// variables to pass.
const variables = {
playid: playID
};
//The request.
const result = $http.post(
/graphql,
{
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ playsQuery, variables })
}
);
return result && result.data;
};
You appear to be sending the data body as:
{
playsQuery: "<query string>",
variables: {}
}
The GraphQL spec specifies that you must follow this format for GraphQL queries over REST:
http://graphql.org/learn/serving-over-http/
{
"query": "...",
"operationName": "...",
"variables": { "myVariable": "someValue", ... }
}

Jasper Async Report - Empty, why?

Lets start , firstly I get parameters for specific report for exmaple
var config = {
url : "http://exmaple.com/jasperserver/rest_v2/reports/reportFolder/exampleReport/inputControls",
method: "GET",
headers: {
Accept: "application/json;"
},
}
Everything is ok, I get a response with an array of inputs,
The array contains, 2 objects with :
First:
{
description: "Date_from",
id: "Date_from",
label: "Date from",
type: "singleValueDate"
}
Second:
{
description: "Date_to",
id: "Date_to",
label: "Date_to",
type: "singleValueDate"
}
Both inputs have a property:
validationRules[0].dateTimeFormatValidationRule.format = "yyyy-MM-dd"
So now I want to run a async report (I will pass async parameter false for now, se there is less code here)
var params ={
reportUnitUri: "/reportFolder/exampleReport",
outputFormat: "html",
freshData : true,
saveDataSnapshot : false,
ignorePagination: true,
async : false,
interactive: false,
allowInlineScripts: true,
parameters: {
"Date_from":["2014-08-01"],
"Date_to":["2015-10-08"]
}
}
So now I try to generate async report:
var config = {
url : "http://exmaple.com/jasperserver/rest_v2/reportExecutions",
headers: {
Accept: "application/json"
},
data: params,
method: "POST"
}
I get a success response, but
totalPages: 0,
requestId: "0200cf28-300f-4e76-b99e-e479be4980ba",
reportURI: "/reportFolder/exampleReport/",
status: "ready",
exports[0].id: "c9a5578a-6bc8-4c3e-8a78-9056ef19f456",
exports[0].status: "ready",
exports[0].outputResource :{
contentType: "text/html",
outputFinal: true
}
When I try to get the output of the report by calling :
http://exmaple.com/jasperserver/rest_v2/reportExecutions/0200cf28-300f-4e76-b99e-e479be4980ba/exports/c9a5578a-6bc8-4c3e-8a78-9056ef19f456/outputResource
The report is empty.
Runing the same via :
http://exmaple.com/jasperserver/rest_v2/reports/reportFolder/exampleReport.html?Date_from=2014-08-01&Date_to=2015-09-08
Gives me a filled report,
Can anyone point me what I'm doing wrong ? :/
I'm pretty sure that it may be something wrong with the parameters but I tried in various ways and I can't find a solution by myself :/
The thing was that the parameters passed to the async report were badly formatted, it should be like this:
parameters: {
reportParameter: [
{name : "Date_from",value : ["2014-08-01"]},
{name : "Date_to",value : ["2015-10-08"]}
]
}

Consuming API in javascript without using Node.js

i am leveraging CamFind's API for image recognition in my windows phone 8 app. On their site they have given an example for how to use the API with Node.js.. however i am writing a PhoneGap Windows Phone app and dont have this availble.
I would like to use just plain jquery/javascript to use this API.
Here's the example provided on their site:
var Request = unirest.post("https://camfind.p.mashape.com/image_requests")
.headers({
"X-Mashape-Authorization": "Z**********************"
})
.send({
"image_request[locale]": "en_US",
"image_request[language]": "en",
"image_request[device_id]": "<image_request[device_id]>",
"image_request[latitude]": "35.8714220766008",
"image_request[longitude]": "14.3583203002251",
"image_request[altitude]": "27.912109375",
"focus[x]": "480",
"focus[y]": "640",
"image_request[image]": "/tmp/file.path"
})
.end(function (response) {
console.log(response);
});
Here's how i am trying to do the same using jquery/ 'plain' javascript
$.ajax({
url: 'https://camfind.p.mashape.com/image_requests', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
type: 'POST', // The HTTP Method
data: {
"image_request[locale]": "en_US",
"image_request[language]": "en",
"image_request[device_id]": "<image_request[device_id]>",
"image_request[latitude]": "35.8714220766008",
"image_request[longitude]": "14.3583203002251",
"image_request[altitude]": "27.912109375",
"focus[x]": "480",
"focus[y]": "640",
"image_request[image]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
}, // Additional parameters here
datatype: 'json',
success: function(data) { alert(JSON.stringify(data)); },
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "Z**********************");
}
});
Issue/Question:
When i do it through javascript/jquery - it seems to be complaining about missing image_request[image] attribute. It never hits the success block.
Am i doing something wrong in terms of how i transformed the Node.js API request example (1st block of code above) provided by CamFind VS. how i am doing trying to consumer the API through plain through Javascript (2nd block of code above)?
Thanks!!
Fyi, references i am using:
Consume an API in javacstipt: https://www.mashape.com/imagesearcher/camfind#!endpoint-1-Image-Request
CamFind API usage: https://www.mashape.com/imagesearcher/camfind#!endpoint-1-Image-Request
I know this is an old question but having stumbled across it whilst trying to solve it myself I thought I should answer it for the future.
The issue is this line:
"image_request[image]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
It should be:
"image_request[remote_image_url]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
So the complete code is:
$.ajax({
url: 'https://camfind.p.mashape.com/image_requests', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
type: 'POST', // The HTTP Method
data: {
"image_request[locale]": "en_US",
"image_request[language]": "en",
"image_request[device_id]": "<image_request[device_id]>",
"image_request[latitude]": "35.8714220766008",
"image_request[longitude]": "14.3583203002251",
"image_request[altitude]": "27.912109375",
"focus[x]": "480",
"focus[y]": "640",
"image_request[remote_image_url]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"
}, // Additional parameters here
datatype: 'json',
success: function(data) { nowDoSomethingFun(data); },
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Key", "YOURKEY")
}
});
}
I am not familiar with this API but you might try formatting your data parameter like this:
data: {
image_request: {
locale: 'en_US',
language: 'en',
device_id: '<image_request[device_id]>',
latitude: '35.8714220766008',
longitude: '14.3583203002251',
altitude: '27.912109375',
image: 'http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg'
},
focus: {
x: '480',
y: '640'
}
}

Categories

Resources