GitHub API AJAX POST returning 422 - json

I'm trying to post an issue via the GitHub API and keep getting a 422 error. I've tried various approaches over the past few days with no luck - I'm hoping this is a simple mistake that someone can spot quickly?
My call is below - I'm using my Personal Access Token for authorization.
$.ajax({
type: "POST",
url: `https://api.github.com/repos/MYUSERNAME/MYREPONAME/issues?access_token=MYACCESSTOKEN`,
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
"title": "Found a bug",
"body": "I'm having a problem with this."
})
}).done(function( data ) {
console.log( data );
});
Thanks in advance.

FYI finally got this to work with the code below to POST to the GitHub API (creating an issue):
$.ajax({
url: 'https://api.github.com/repos/USERNAME/REPONAME/issues',
type: 'POST',
dataType: 'json',
headers: {
Authorization: 'token MY_PERSONAL_TOKEN'
},
data: JSON.stringify({
"title": "Found a bug",
"description": "Bug description"
}),
success: function (response) {
console.log(response);
}
});

Related

Symfony $request->getContent() in bad format?

i send a request to my symfony backand via
$.ajax({
url: "/example/example",
method: "POST",
data: { test: "hallo", test2 : "hallo2" },
contentType: "application/json; charset=utf-8",
dataType: "json"
...
but my i become in controller when i use $request->getContent() the following output:
test=hallo&test2=hallo
but i need the json formatted content, how i can get this from the request? Like so:
[
{"test" : "hallo"},
{"test2" : "hallo2"}
]
You are not sending JSON data to your backend, you can use JSON.strinify to do this.
var data = [
{"test" : "hallo"},
{"test2" : "hallo2"}
];
jsonData = JSON.stringify(data);
$.ajax({
url: "/example/example",
method: "POST",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json"
...

Sending a post request through ajax with data as json, but in the endpoint it is not coming as json

I am building a simple post request using ajax and nothing fancy about that:
$.ajax({
type: 'POST',
contentType: "application/json",
dataType: 'json',
url: 'https://nnn.beeceptor.com',
data:{
country: "country",
customer_title: "customer_title",
date_of_birth: "date_of_birth",
education: "education",
email: "email",
first_name: "first_name",
institution: "institution",
last_name: "last_name",
pay_type: "pay_type",
phone: "phone",
way_of_contact: "way_of_contact",
work_experience: "work_experience"
},
success: function(data){
console.log(`success`)
},
error: function(data){
console.log(`error`);
}
})
Now I want in the endpoint that data that I am sending in the body to come as json.
How can I fix that so the data comes to the endpoint formatted as json. For tests you can use that endpoint to see the format in which the data is coming. Any suggestion is much appreciated.
After some research I was able to come with an answer. On the ajax request was needed to define the content in the header as follows:
headers: {
"content-type": "application/json;charset=UTF-8" // Add this line
},
and in the data attribute:
data: JSON.stringify(data_object)
That solved my problem

WinJs xhr getting error The request entity's media type 'text/plain' is not supported for this resource

WinJs xhr getting error The request entity's media type 'text/plain' is not supported for this resource,
I am sending json to a webapi , I have set the header to application/json so I was not expecting this error.
any help here thanks
WinJS.xhr(
{ url: "http://api.xxxxxx.com.au/api/jobs/GetNearActiveJobs", type: "POST", responseType: "json", data: { LocationId: 23555, kms: 10 }, headers: { contentType: "application/json" } } )
Try this
WinJS.xhr({
type: "post",
url: "http://api.xxxxxx.com.au/api/jobs/GetNearActiveJobs",
headers: { "Content-type": "application/json" },
data: JSON.stringify({ LocationId: 23555, kms: 10 })
})

Ajax call throws parse error

I am getting a parse error when trying to perform the following call:
$.ajax({
cache: true,
url: "http://localhost:3000/app/test/result1",
data: "{}",
type: "GET",
jsonpCallback: "testCall",
contentType: "application/jsonp; charset=utf-8",
dataType: "jsonp",
error: function (xhr, status, error) {
alert(error);
},
success: function (result) {
alert(result);
},
complete: function (request, textStatus) {
alert(request.responseText);
alert(textStatus);
}
});
If I paste the request url in the browser address bar directly, the result returned is seems to be valid JSON:
{
"name": "The Name",
"description": "The Description"
}
I am using IISnode, NodeJs & Express JS. I have tested both scenarios:
// NODE JS CODE
var app = express();
app.get('/app/test/result1', function (req, res) {
res.send({ name: "The Name", description: "The Description" });
});
app.get('/app/test/result2', function (req, res) {
res.send(JSON.stringify({ name: "The Name", description: "the Description" }));
});
Any advice is appreciated, thanks in advance.
Your dataType is listed as jsonp. However, your response from node.js is not a properly formatted JSON-P response. JSON-P responses need to be wrapped in a callback, like so:
testCall({ name: "The Name", description: "The Description" });
The node.js code would look something like this:
res.send(
'testCall(' +
JSON.stringify({ name: "The Name", description: "the Description" }) +
');'
);
(Also note that you don't need the data: "{}" line since the request is a GET request and doesn't include any data. It shouldn't hurt anything, but might be nice to remove to avoid confusion).

how connect with web service

I am trying to get some data from a web service:
$.ajax({
type: 'POST',
url: "http://192.******.asmx?op=GetJSONString",
method: "serverCallback",
data: "Select con.cod as codigo, con.res as descripcion from con where ide>0",
success: function(data){alert(data)},
});
How can I get the returned JSON data?
Here is what my calls look like. Can you be more specific of the issue you are having?
$.ajax({
type: "POST",
data: "SOME DATA",
dataType: "json",
url : "/myapp/service.json",
cache: false,
error: function() {
alert("There was an issue");
}
,
success: function(data)
{
processJson(data);
}
});
I will also post what has worked with me for asmx services.
In the example below, for "url": "/service/Details.asmx/Reject", the "/Reject" is the method name in the web service (asmx) file.
$.ajax({
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"type": "POST",
"url": "/service/Details.asmx/Reject",
"data": "{\"itemId\":\"" + id + "\",\"comment\":\"" + comms + "\"}",
"success":
function (msg) {
alert("Yea it worked!");
});
}
});