How to summarize/resume json object in another json object - ruby

How can I summarize a json data inside another json data, that is, put all the key:value ​​inside another json without having to define again.
Ex:
default_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'Content-Type': 'application/json'
}
special_headers = { default_headers, 'Authorization': 'Bearer xxxxxxx'}
# special_headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
# 'Content-Type': 'application/json', 'Authorization': 'Bearer xxxxxxx' }

You can Hash#merge the new hash into the one you already have:
{
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'Content-Type': 'application/json'
}.merge('Authorization': 'Bearer xxxxxxx')
# {:"User-Agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36", :"Content-Type"=>"application/json", :Authorization=>"Bearer xxxxxxx"}

Related

POSTing JSON data to WCF Rest Service

I know there are many questions on SO about this but none of the suggestions have worked for me.
Here is my code:
var restService = "http://wcfrestservice:8004/RADPOCService/WebApp1";
$.ajax({
url: restService,
type: "POST",
data: { PhoneNumber: y },
dataType: "json",
contentType: "application/json; charset=utf-8",
success:
function (data) {
window.open(data.Url, '_blank');
}
});
Fiddler shows my request going across the wire like so (redacted):
POST http://localhost:8004/RADPOCService/WebApp1 HTTP/1.1
Host: localhost:8004
Connection: keep-alive
Content-Length: 22
Accept: application/json, text/javascript; q=0.01
Origin: http://localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: http://localhost:8000/Default.aspx
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Requestor: Me
PhoneNumber=1234567890
I am expecting the payload to look like this:
{ "PhoneNumber": "1234567890"}
When I set the payload as above in the Fiddler Composer tab the service works as expected. What am I doing wrong?
I hate to do this so quickly after posting my question but the answer is to use the JSON.stringify API on the data being sent to the service. So the above should have the following code for 'data' in the ajax call:data: JSON.stringify({ PhoneNumber: y })

How to download a file using Parse.Cloud.httpRequest for content type zip

I wanted to create a scheduled Parse job to download a zip file. I tried it with Parse.Cloud.httpRequest(), but it seems like it works only for json and url encoded content type.
How exactly can I get this done from cloud code?
Parse.Cloud.define("getPage", function(request, response) {
Parse.Cloud.httpRequest({
url: 'http://www.nseindia.com/content/historical/EQUITIES/2015/MAY/cm12MAY2015bhav.csv.zip',
headers: {
"Content-type": "application/zip",
"Expires": "0",
"Content-disposition": 'attachment; filename="cm12MAY2015bhav.csv.zip"',
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36"
},
success: function(httpResponse) {
console.log("Response-text:"+ httpResponse.text);
console.log("Response-text:"+ httpResponse.data);
//response(httpResponse.text);
},
error: function(httpResponse) {
console.log("Error hit, error: "+ httpResponse.text);
//response('Request failed with response code ' + httpResponse.status);
}
});
});
The above code gives out "Undefined" for httpResponse.data.
Data is going to be JSON or something of the sort. If it's a file, use httpResponse.buffer.

How to delete table (class) from my parse.com account

Since there is no parse.com API to delete a class, how should one delete a class in one call? the recommended way is to delete row by row and then the class is left empty but is there an easier way?
var options = {
method: 'POST',
url: 'https://api.parse.com/1/schemas/' + ___TABLE_NAME___,
body: '{"_ApplicationId":"___APP_ID___","_MasterKey":"___MASTER_KEY___","_ClientVersion":"browser","_method":"DELETE"}',
headers: {
'Pragma': 'no-cache',
'Origin': 'https://www.parse.com',
'Accept-Encoding': 'gzip,deflate',
'Host': 'api.parse.com',
'Accept-Language': 'en-US,en;q=0.8,he;q=0.6',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
'Accept': '*/*',
'Cache-Control': 'no-cache',
'Referer': 'https://www.parse.com/apps/COLLECTION_NAME/collections',
'Connection': 'keep-alive',
'Content-Length': '163'
}
};
request(options, function(r,o,t) {
callback();
})

Ajax post sometimes removes body and adds NTLM Authorization

We're experiencing some problems with some ajax calls. On the server side we're running Apache Tomcat with Servlets. On a few calls an Authorization (NTLM) is added in the request header,
and the post body is removed. We are using NTLM authentication on the site, but authentication is already done before these ajax calls are made, and this only happens on certain ajax calls.
Here the JavaScript making the Ajax call.
var postObjects = function(f, parameter, value, variables)
{
var post = {};
post['f']=f;
post['courseid']=trapi.courseID;
post['courseresourceid']=trapi.courseResourceID;
post['mode']=trapi.mode;
if(parameter!=null)
post['parameter']=parameter;
if(value!=null)
post['value']=value;
if(variables!=null)
{
for(var i=0; i<variables.length;i++)
{
post[variables[i][0]]=variables[i][1];
}
}
var returnString="";
$.ajax(
{
url : location.pathname,
data:post,
cache:false,
global:false,
dataType:'text',
contentType:'application/x-www-form-urlencoded; charset=UTF-8',
type:'POST',
async:false,
success: function(data)
{
returnString=data;
},
error: function(jqXHR, textStatus,errorThrown)
{
returnString="Error: "+textStatus;
}
});
return returnString;
}
And here is info from fiddler about a POST made by the postObjects function which adds NTLM authorization:
POST http://localhost:8080/trainweb/courses HTTP/1.1
Accept: text/plain, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:8080/trainweb/courses?f=courseframe&courseid=909
Accept-Language: nb-NO
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Connection: Keep-Alive
DNT: 1
Host: localhost:8080
Pragma: no-cache
Cookie: JSESSIONID=C18B2FA564626BCEB82C4C3AD8837AE8; FillScreenWidth=0; DefaultSearch=docno; lang=no
Authorization: NTLM BASE64ENCODEDSTRING
Content-Length: 0
And here is info from fiddler about a POST made by the same postObjects function not adding NTLM authorization:
POST http://localhost:8080/trainweb/courses HTTP/1.1
Accept: text/plain, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:8080/trainweb/courses?f=courseframe&courseid=909
Accept-Language: nb-NO
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Connection: Keep-Alive
Content-Length: 61
DNT: 1
Host: localhost:8080
Pragma: no-cache
Cookie: JSESSIONID=C18B2FA564626BCEB82C4C3AD8837AE8; FillScreenWidth=0; DefaultSearch=docno; lang=no
f=getlasterror&courseid=909&courseresourceid=4079&mode=normal
Do I have to re-authenticate each time this happens?

Run jQuery AJAX call when previous AJAX call fail due to connection was unexpactly closed

I know about error in application running on server and invoked operation disconnect all HTTP clients. I need to invoke this functionality on the server but I'm not able to fix the error.
Here is my server code:
<?php
// file: run_script.php
shell_exec('close_all_HTTP_connections_error '.$_REQUEST['params']);
?>
I'm trying to find workaround via AJAX call, I know that call fails, but user doesn't see error like ERR_EMPTY_RESPONSE. My problem is that I need to do another call to the server (which doesn't fail normaly). The second call is not send to server because previous call fail I guess.
Here is my jQuery AJAX call:
function sendData(url, step) {
$.ajax({
type: 'POST',
async: true,
url: url,
headers: { // I try it without this also
'Connection' : 'close'
},
data: {
'params' : 'bla bla'
},
complete: function(jqXHR, textStatus) {
console.log(jqXHR);
console.log(textStatus);
if (step < 2) {
sendData('another.php', step+1);
}
else {
console.log("done");
// go forward
}
},
dataType: 'html'
});
}
$(document).ready(function() {
sendData('run_script.php', 1);
});
Here is my screen shot from Chrome inspector:
And HTTP Requests:
run_script.php:
POST http://?????.com/test/run_script.php HTTP/1.1
Origin: http://?????.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.22 (KHTML, like Gecko)
Chrome/19.0.1049.3 Safari/535.22
Content-Type: application/x-www-form-urlencoded
Accept: text/html, */*; q=0.01
Referer: http://?????.com/test/result.php
params:bla bla
another.php:
POST http://?????.com/test/another.php HTTP/1.1
Origin: http://?????.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.22 (KHTML, like Gecko)
Chrome/19.0.1049.3 Safari/535.22
Content-Type: application/x-www-form-urlencoded
Accept: text/html, */*; q=0.01
Referer: http://?????.com/test/result.php
params:bla bla
There is no HTTP Response of course.
Problem was, that task previously invoked on server restarts server. Then I need to wait before next AJAX call is invoked.
I fact I have to solve problem with server restart first, this is work-around only and not the best solution.

Resources