JS Node send JSON on PATCH with XMLHttpRequest - javascript

I try to send a JSON on a request on POST but it's not working
The code:
let req = new XMLHttpRequest();
req.open("PATCH", "https://discord.com/api/v8/users/#me/settings", true);
req.setRequestHeader("authorization", token);
req.setRequestHeader("content-type", "application/json");
req.send(JSON.stringify({custom_status: "hello"}));
it's need to send on JSON.stringify custom_status: with the text "hello"
When i try this, it's don't give me any error / logs it's show nothing but it's not working
if someone can help me, thanks in advance

Related

XMLHttpRequest sends b'' instead of json data

I'm trying to send json data from my web app to my python flask api, my server is receiving the POST rqeuest & is receiving data, the issue is that it's not receiving json data rather the string b''
This is the javascript code I got to send the json data to my api
function onSubmit() {
document.getElementById("orderbutton").disabled=true;
setTimeout('document.getElementById("orderbutton").disabled=false;',3000);
var request = new XMLHttpRequest();
var url = "http://127.0.0.1:5000/api/order";
request.open('POST', url, true)
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.onload = function() {
var response = JSON.parse(request.responseText)
console.log(response)
}
request.send(JSON.stringify({"id": document.getElementById("order").value.trim()}))
}
edit: it's not an issue with document.getElementById("order").value.trim() because when I console.log it, I get the input I put in my <input> field
Try editing your send() line to
request.send(JSON.stringify({id: document.getElementById("order").value.trim()}));
Change is from "id" to id
use .decode('utf-8'), Let me know the result.

Making a simple POST request with the pastebin API

I was wondering how I would be able to make a post request to pastebin.com. They have an easy to understand API documentation, but whenever I run a simple POST request I always get Bad API request, invalid api_option.
I'm using the bear minimum required to make the request, I'm not sure what I could be doing wrong.
var request = new XMLHttpRequest;
request.open("POST", "https://pastebin.com/api/api_post.php?api_dev_key=mykey&api_option=paste&api_paste_code=hi", false);
request.send();
request.response;
Finally I made it work like this:
var request = new XMLHttpRequest();
request.open("POST", "https://pastebin.com/api/api_post.php", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send("api_dev_key=YOUR_KEY_HERE&api_option=paste&api_paste_private=0&api_paste_name=myname.js&api_paste_expire_date=10M&api_paste_format=javascript&api_paste_code=random");
I hope it helps

Can't send file using XMLHttpRequest

The code is simple and it works when I append a string to the FormData:
var data = new FormData();
data.append('String', stringVar);
var request = new XMLHttpRequest();
request.open('POST', 'upload.php');
request.send(data);
But if I append a file to the FormData (data.append('File', fileVar);) I get a 503 response.
The problem doesn't happen when I'm running the code on localhost, so I know there's no problem with the code and it's from the server. Maybe I should change some PHP or Apache settings on the server, I don't know.
Any help or comments would be appreciated.

What should a proper GET request and response look like in Node.js

I am working on a small project using Node.js.
The objective is to send an HTTP Request to an array of websites and display what they return to me.
First someone helped me to figure out that I needed a specific Node.js module (XMLHttpRequest). So I "required" it after installing it with NPM. Then I instantiate it.
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
//I don't think I need this
xmlHttp.send(null);
//Log some stuff to the console, namely the "response"
console.log(xmlHttp.responseText);
console.log(xmlHttp.statusText);
console.log("Finished!");
Now I believe what this will do is send a GET message to "theUrl", and then save the response in the xmlHttp Object's responseText member.
So now I should have a response. I should be able to print that as text (console.log(xmlHttp.responseText);). What should be in this response?
I expect to get something like "200 OK" but that is nowhere in the response I get. Am I going about this in the right way?
I plan on using the Async Node.js module to send a request like this to an array of URLs, trim up their response (name of the website name, the response status code, and each of the response headers).
You can use below;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
// responseText => response body as string
// status => 200 is OK, 404 page not found
}
};
xhr.open("GET", "yor_url");
xhr.send();
responseText: response body as string
status: 200 is OK, 404 page not found

Pastebin.com Post

I'm trying to post a new Pastebin through a popup window in Javascript. The issues I'm getting is it is saying "Bad API request, invalid api_option"
Link that I'm using:
http://pastebin.com/api/api_post.php?api_dev_key=<KEY>&api_paste_name=T‌​ITLE&api_option=paste&api_paste_code=SOMETEXT
It says to put api_option as paste. I've tried looking up other examples, but no luck yet. Has everyone ran into this problem?
Are you, by any chance, required to POST the data rather than GETting it?
Also, it might not be the best idea ever to put your API key on the internet like this.
How are you submitting this request to Pastebin? Is it via POST or GET? My best guess is that you're sending a GET request and the API requires a POST.
Try this:
let api = {
option: "paste",
user_key: "XXXXXXXXXXXX",
dev_key: 'XXXXXXXXXXXX',
paste_name: "MyTitle",
paste_format: "JSON",
paste_private: 0,
paste_code: ""
};
let request = new XMLHttpRequest();
request.open('POST', 'http://pastebin.com/api/api_post.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
data['test'] = 'Yeah PasteBin!';
dataString = 'api_option='+api.option+'&api_user_key='+api.user_key+'&api_dev_key='+api.dev_key+
'&api_paste_name='+api.paste_name+'&api_paste_format='+api.paste_format+
'&api_paste_private='+api.paste_private+'&api_paste_code='+data;
request.onreadystatechange = function() {
if (request.status == 200 && request.readyState == 4) {
alert("URL to new pastebin file: " + request.responseText);
}
}
request.send(dataString);
The main issue with your code is put everything in your request URL, what is fine if it is a GET request. The URL of PasteBin: api/api_post.php demands a POST request (notice the name?), so you have to send it in the body like I've shown you above.

Categories

Resources