AJAX Post Request with Raw HTTP Request - Javascript - javascript

I am looking for a way to send the following POST request using only plain Javascript:
Let's say the above HTTP request was a variable, would it maybe be possible to send that as a request and gather the result? Or is it a better way to send this POST request using Javascript?
Thanks in advance for all answers.

Something like this should work as long as it is your own api that you are calling to?
var post = function() {
var url = "http://www.example.com";
var request = new XMLHttpRequest();
request.open( "POST", url, true);
request.send(null);
}

Related

Why do we need to give url while sending post request via XMLHttpRequest?

const xhr= new XMLHttpRequest();
xhr.open('POST',url,true);
Let params="name"="Rahul"
xhr.send(params)
Cannot understand why we use url in post request

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

Post request to webhook without redirecting

How can I make a post request (after validating some user input) to a webhook without redirecting the user to the webhook url?
Tis kind of request is generally done using AJAX.
If you are using jQuery, you should look at .post() method. See an exemple of it:
$.post( "/url.html", function( data ) {
# action on request response
});
If you are using raw javascript (without js libs), you should look at XMLHttpRequest() object. Example of use:
var post = function() {
var url = "http://www.example.com";
var request = new XMLHttpRequest();
request.open( "POST", url, true);
request.send(null);
}
Using this approach of request you will not be redirect, like the common case of HTML submitions.

XMLHttpRequest Returning Bad Request (400) while calling from java script , if i am calling from Java or Postman client it working fine

I am trying to call Spring rest method using java script.
I am getting:
POST http://www.davco.com/search/loginAuthentication 400 (Bad Request)
Below is my Java scrpt Code to invoke Spring REST service that I have
var userEmailId= $("#emailAddress").val();
var userPwd= $("#userPassword").val();
if (empty(userPwd)) {
alert("Please enter password");
return false;
}
var http = new createXMLHttpRequest();
var url = "/search/loginAuthentication";
var params = 'eid=' +userEmailId+'&pwd='+userPwd
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/xml");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
When i am trying to call same method from Google Postman client I am able to hit services and get the response..
I am not able to understand what wrong i am doing while i am calling it form Javascrpt. I have refer this link, this link as well.
I tried different Content-type header like :
http.setRequestHeader("Content-type", "text/html");
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Content-type", "Accept=application/xml");
I am seeing from browser request and request payload for POST call is going like below that is correct :
Below is my Spring Rest Services Code:
#RequestMapping(method = RequestMethod.POST, value = "/loginAuthentication" ,headers = "Accept=application/xml, application/json, text/html")
public #ResponseBody String loginAuthnticationForHTEtb(#RequestParam("eid") String userEmailId,#RequestParam("pwd") String password
,HttpServletRequest request, HttpServletResponse response) {
StringBuffer result = new StringBuffer();
try {
String domainVraiable=Context.getDomainName();
int inxexOfDot=domainVraiable.indexOf(".")+1;
int lastIndexOf=domainVraiable.lastIndexOf(".");
....
I am just wondering why it working and i am able to call from PostMan Client but not working while i am trying to call from java script? As you can see in below screenshot.
Thanks in advance for any kind of clue or information to get rid of this. I spend almost a day but could not figured out.
try it:
var params = {
eid:userEmailId,
pwd:userPwd
};
http.send(params);
or:
var formData = new FormData();
formData.append("eid", userEmailId);
formData.append("pwd", userPwd);
http.send(formData);
After spending a lot of time,still I am not able to figure out what exactly the problem in above implementation.
But now I change like this and working fine with below style :
var response = $.post("/search/loginAuthentication",{"eid":userEmailId, "pwd" :userPwd},
function(data,status){
processAuthenticationResponse(data);
},'json');
}
I had same problem. But then I sent string of JSON instead of JSON object. Then it worked.
request.send(JSON.stringify(json));

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