I have to hit a URL and get its encrypted data response back and use that encrypted data to decrypt data bu using a key.
I used POSTMAN to get the data response back but when I looked into it was just some symbols and not anything like a data response. So, it is encrypted.
I already have a function that convert the encrypted response back to plain text but now, I am not understanding how will I convert that data response to plain text as I have to first get that data response and then only use that data response in a parameter of a decrypto function and with the help of key I can change the back it to plain text.
I know how to change a cipher text to pln text but here things are little bit different.
But as I have to get the data response back shouldn't I have to make a POST request to get it or maybe I am understanding it wrong.
This is my decrypto function-
function decryptByDES(cipherTextString, keyString) {
var keyHex = CryptoJS.enc.Utf8.parse(keyString);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(cipherTextString)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
alert(decrypted);
return decrypted.toString(CryptoJS.enc.Utf8);
}
Here when I calling it
<button onclick="decryptByDES('aHJHDJSHJhjsak=', 'ALSOWURNsa');">View</button>
I am giving or specifying the ciphertext string and key string as I using it only for testing there is no security issue. It is giving the decrypted value in alert-box.
So, in all I want to know how to get an encrypted data response and used that in the function so read it like a plain text.
EDIT:
With the help of POSTMAN I generated code for Javascript Ajax call
var settings = {
"async": true,
"crossDomain": true,
"url": "http://192.168.168.76:8080/HTMLPortingNewService/GetData?ChartName=widget3LineChart&lob=M&carrier=0&enrollmenttype=0&state=0&agent=0&fromdate=04%2F03%2F2015&todate=05%2F03%2F2015&requestID=499F6BF5E4610454A887AB37AF0814E8",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "ac20a050-a8c8-6d58-4350-66141d519394",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"username": "aHRtbHVzZXIx",
"password": "SHRtbDIwMTY="
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
So, now how I can call this response in my function??
You have to call decryptByDES after ajax complete.
function callServer(){
var settings = {
"async": true,
"crossDomain": true,
"url": "http://192.168.168.76:8080/HTMLPortingNewService/GetData?ChartName=widget3LineChart&lob=M&carrier=0&enrollmenttype=0&state=0&agent=0&fromdate=04%2F03%2F2015&todate=05%2F03%2F2015&requestID=499F6BF5E4610454A887AB37AF0814E8",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "ac20a050-a8c8-6d58-4350-66141d519394",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"username": "aHRtbHVzZXIx",
"password": "SHRtbDIwMTY="
}
}
$.ajax(settings).done(function (response) {
console.log(response);
decrypted = decryptByDES(response, keyString);
console.log(decrypted);
});
}
And in your HTML call this function:
<button onclick="callServer();">View</button>
Related
I recently tested A LOT of different syntax for sending an ajax post request, and the only one which actually worked was the one I got from my Postman testing. The only problem I have now is the Postman code snippet is "hardcoded" and features a weird string syntax I have never seen before. I want to replace parts of that weird string with values from HTML inputs. Any ideas how I can achieve this?
I have two simple HTML inputs:
<input type="text" id="username" name="username" placeholder="Username" autofocus />
<input type="password" id="password" name="password" placeholder="Password" />
And here is the part I got from Postman (JavaScript):
var settings = {
"async": true,
"crossDomain": true,
"url": "...",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"cache-control": "no-cache"
},
"processData": false,
"data": "{ \r\n\"Username\":\"username123\",\r\n\"Password\":\"password123\"\r\n}"
}
$.ajax(settings).done(function(response) {
console.log(response);
});
Specifically, the part I'm asking about is the "data" line. I want to be able to take the values from the two HTML inputs, and replace "username123" and "password123" with them respectively. I have tried playing around with the data string, like removing \r\n and some " and \ , but then I get an error from the API I'm trying to call to. Do I really need all those? To be clear, I'm wondering how to put my variables into the string using valid syntax, not how to get the values like for example:
var usname = document.getElementById("username").val();
or
var usname = $('#username').val();
And these JSON syntax are tested and recieves an error:
"data": {"Username": "username123","Password": "password123"}
"data": "{"Username": "username123", "Password": "password123"}"
"data": {"Username": usname, "Password": pword}
"data": {"Username": $('username').val(), "Password": $('password').val()}
I feel like at least the first one should work, but it just comes back with error 500. For reference, here's what the body (data) looks like in Postman (working):
{
"Username":"username123",
"Password":"password123"
}
Could it be an issue with whitespace or something? I sadly don't have access to the source code of the API I'm calling.
I'm wondering how to put my variables into the string using valid
syntax
settings.data = settings.data.replace("username123", usname);
settings.data = settings.data.replace("password123", uspassword);
For formatting the data as JSON and the be able to use its properties for different purposes:
var settings = {
"async": true,
"crossDomain": true,
"url": "...",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"cache-control": "no-cache"
},
"processData": false,
"data": "{ \r\n\"Username\":\"username123\",\r\n\"Password\":\"password123\"\r\n}"
};
//remove line breaks
settings.data = settings.data.replace('/\r?\n|\r/', '');
//convert to properties
settings.data = JSON.parse(settings.data);
//re-assign properties as needed
settings.data.Username = 'newUsername';
settings.data.Password = document.getElementById('password').value;
console.log(settings.data);
<input type="password" id="password" name="password" placeholder="Password" value="newPassword"/>
I suggest to use a FormData to wrap the data you are going to send:
var formData = new FormData();
formData.append('Username', $("#username").val());
formData.append('Password', $("#password").val());
And later, you call the ajax post like this:
var settings = {
"async": true,
"crossDomain": true,
"url": "...",
"method": "POST",
"contentType": 'application/x-www-form-urlencoded',
"processData": false,
"data": formData
};
$.ajax(settings).done(function(response)
{
console.log(response);
});
If you need to send data as JSON then you can give a try to next code:
var data = {};
data.Username = $("#username").val();
data.Password = $("#password").val();
var settings = {
"async": true,
"crossDomain": true,
"url": "...",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"cache-control": "no-cache"
},
"processData": false,
"data": JSON.stringify(data)
};
$.ajax(settings).done(function(response)
{
console.log(response);
});
I am trying to call an API from draftable.com (https://api.draftable.com/v1/comparisons) using Google App Script. The API takes two files and returns a string. I have made the GET and DELETE functions which are working fine, but when I run a POST function I am getting an error at the
UrlFetchApp.fetch("URL",object);
Error: Request failed for https://api.draftable.com/v1/comparisons
returned code 400. Truncated server response: {"left":["This field is
required."],"right":["This field is required."]}
I am passing a JSON like the code below:
function POST() {
var options = {
"method": "POST",
"headers": {
"Content-Type": "multipart/form-data",
"Accept": "application/json",
"Authorization": "Token {iputmytokenhere}",
},
"left": {
"source_url": "https://api.draftable.com/static/test-documents/paper/left.pdf",
"display_name": "old-example.docx",
"file_type": "pdf"
},
"right": {
"source_url": "https://api.draftable.com/static/test-documents/paper/right.pdf",
"display_name": "newer-example.docx",
"file_type": "pdf"
}
};
var result = UrlFetchApp.fetch("https://api.draftable.com/v1/comparisons", options);
var params = JSON.parse(result.getContentText());
Logger.log(params.identifier);
}
}
Can anyone tell me why is the error coming.
A client wants to be able to make xmlhttp ajax requests with the default content type of content-type:"application/x-www-form-urlencoded; charset=UTF-8" but sending the data in the form the API expects application/json. So the request comes across as this:
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:80/api/metadata/taxonomy",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "62a245ad-a0a2-4dd3-bf84-37f622f00b7d"
},
"processData": false,
"data": "{\n\t\"practice\": [\"Learning\"]\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
But the API expects to be able to get req.body as a JSON object it can immediately use:
"{"practice":["Learning"]}"
Can I transform this "{\n\t\"practice\": [\"Learning\"]\n}" to this "{"practice":["Learning"]}" in some safe/suggested manner? (without some home grown parsing function or regex)
Yes, the JSON.parse function can be used for this:
try{JSON.stringify(JSON.parse(data))}
will convert strange json with newlines to standard one line string json.
JSON.parse
Parses the json into an object
JSON.stringify
Turns an object into a one line formatted JSON object
try
JSON.parse will fail if an invalid json string is passed. (\ns are valid in json.parse)
If this you meant converting "{\n\"your\":\n \"object\"}" to a javascript object like {"your": "object"}, you can just use try{JSON.parse(variablename)}
According to these answers, for older browsers you may need JSON-js
You can actually post JSON as body.
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:80/api/metadata/taxonomy",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "62a245ad-a0a2-4dd3-bf84-37f622f00b7d"
},
"dataType" : "json"
"processData": false,
"data": { "practice": ["Learning"] }
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Notice that theirs a new property which tells that data type to be posted is JSON and and now in data property the value is in JSON format instead of string.
Hope this solves your problem.
I've created an Asp.Net Core Web Application and am looking to implement a Web API.
I've got a functional HttpGet implemented and am now trying to implement a HttpPost function:
[HttpPost]
public object Post([FromBody] object data)
{
return null;
}
I've tested this using Postman which worked fine.
I set it up with json object in the body (set to raw and JSON (application/json)).
I get the expected response.
But when I try to call this from javascript using code suggested by Postman (or my own attempts):
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:51139/api/data",
"method": "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": "999ee8ee-4e92-acb8-b7cf-144ffa49e5ee"
},
"processData": false,
"data": "{\"Message\":\"This is body data\"}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
I get a "415 Unsupported Media Type" error.
Anyone see where I'm going wrong?
I had the same problem and tried a million different combinations ($.ajax, $.post etc), but what finally did it was the accepts: "application/json":
{
type: "post",
accepts: "application/json",
url: "http://localhost:51139/api/data",
contentType : "application/json",
data: "{\"Message\":\"This is body data\"}"
}
You could try something like this
[HttpPost]
public object Post()
{
Object o = Request.Form.First(o => o.Key == "data").Value
return null;
}
Then if you can create a class of that object you can use JsonConvert to Deserialize that object into the class object that you have created.
I have to convert a encrypted data response back to plain text.
For this I am using a ajax post call to get the data response and than on sucess passing that data response a decrypto function and getting back the response in plain text (this is the idea)
Now, when I created a Ajax post call and than passing that decrypto their on success nothing is happening
I am sure my decrypto is working
Here is working fiddle for that
https://jsfiddle.net/yktup39e/
Now, problem is in the Ajax. I have only once make an Ajax call so I pretty much sure there will problem in that. But I am not getting that.
This is my code-
JS -
$('#action-button').click(function() {
var settings = {
async: true,
crossDomain: true,
url: "http://192.168.168.76:8080/HTMLPortingNewService/GetData?ChartName=widget3LineChart&lob=M&carrier=0&enrollmenttype=0&state=0&agent=0&fromdate=04%2F03%2F2015&todate=05%2F03%2F2015&requestID=499F6BF5E4610454A887AB37AF0814E8",
method: "POST",
headers: {
"cache-control": "no-cache",
"postman-token": "ac20a050-a8c8-6d58-4350-66141d519394",
"content-type": "application/x-www-form-urlencoded"
},
data: {
"username": "aHRtbHVzZXIx",
"password": "SHRtbDIwMTY="
}
}
$.ajax(settings).done(function (response) {
console.log(response);
decrypted = decryptByDES(response,DES);
console.log(decrypted);
});
});
function decryptByDES(cipherTextString, keyString) {
var keyHex = CryptoJS.enc.Utf8.parse(keyString);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(cipherTextString)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
alert(decrypted);
return decrypted.toString(CryptoJS.enc.Utf8);
}
HTML -
<script src="tripledes.js"></script>
<script src="mode-ecb.js"></script>
<button id="action-button">Click me to load info!</button>
<div id="info"></div>
Can anyone please help me with that??
Try to include jquery in your page.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>