I'm new to using RapidAPI. I want to pull live Cricket Scores from RapidAPI with Excel VBA, but the programming language isn't available on the platform.
I would like to know if there is anyway I can view the json results directly through a browser. This is what I read in their documentation https://docs.rapidapi.com/docs/getting-started-with-rapidapi-sdks but doesn't seem to solve my problem
"What If the Programming Language I'm Using Isn't Available?
Whether the programming language you're using isn't available or you prefer to use another request library, have no fear! All of the APIs available on RapidAPI are exposed through a REST API endpoint. All you'll need to do is take the information provided on the documentation provided. Here's an example of what that would look like using the 'request' npm module"
var options = {
method: 'POST',
url: API_URL,
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/json',
'X-RapidAPI-Key': API_KEY,
'header1': 'header-value-1'
},
qs: {
parameter1: 'parameter-value-1'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
I'm looking for a better idea of working with RapidAPI in Excel VBA. If I can get the json response directly in a browser that solves my problem. But if that's not possible, any example to setup a Node.JS server offline and possibly display the json response in a browser may also work. Thanks in advance
Const Covid19URL As String = "https://covid-193.p.rapidapi.com/history?day=2020-03-25&country=Malaysia"
Const APIKey As String = "brx2HKOJvP6iQ14WxeumLmnhx2L2MAZz"
Sub Main()
Dim xmlhttp As New MSXML2.XMLHTTP60
Dim xmlresponse As New DOMDocument60
xmlhttp.Open "GET", Covid19URL, False
xmlhttp.setRequestHeader "x-rapidapi-host", "covid-193.p.rapidapi.com"
xmlhttp.setRequestHeader "x-rapidapi-key", APIKey
xmlhttp.send
Cells(2, 12).Value = xmlhttp.responseText
'xmlresponse.LoadXML (xmlhttp.responseText)
'"response":[
'{"country":"Malaysia","cases":{"new":"+172","active":1596,"critical":64,"recovered":183,"total":1796},"deaths":{"new":"+1","total":17},"day":"2020-03-25","time":"2020-03-25T09:15:07+00:00"},
'{"country":"Malaysia","cases":{"new":"+172","active":1597,"critical":64,"recovered":183,"total":1796},"deaths":{"new":null,"total":16},"day":"2020-03-25","time":"2020-03-25T07:15:06+00:00"},
'{"country":"Malaysia","cases":{"new":"+106","active":1425,"critical":64,"recovered":183,"total":1624},"deaths":{"new":"+2","total":16},"day":"2020-03-25","time":"2020-03-25T06:15:05+00:00"}
Set xmlresponse = Nothing
Set xmlhttp = Nothing
End Sub
Related
Hi I'm doing a coding challenge and I'm trying to fetch data from an html page but I keep getting the 400 error. I don't see where my syntax would be wrong. The Promise returns an empty string as PromiseResult. Why do I not get the data from https://adventofcode.com/2021/day/2/input?
fetch('https://adventofcode.com/2021/day/2/input', {
method: 'GET',
mode: 'no-cors',
credentials: 'omit',
headers: {
'Content-Type': 'text/plain'
}
})
.then((response) => {
response.text();
})
.then((html) => {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
})
.catch((err) => { console.log(err) })
After visting https://adventofcode.com/2021/day/2/input I get information that
Puzzle inputs differ by user. Please log in to get your puzzle input.
Please check if you are providing your login data correctly.
mode: 'no-cors',
This says that you are not going to do anything that requires permission to be granted using CORS. Anything that does require CORS will be quietly ignored.
You need CORS permission to read data across origins. Since you have said you aren't going to use CORS, you don't have permission, so you don't get any data.
This is why you get an empty string.
credentials: 'omit',
The input endpoint requires your credentials so that it can give you your input, which is unique to each user. Since told the browser not to send any, the end point doesn't understand the request.
As an aside:
headers: {
'Content-Type': 'text/plain'
}
This is just nonsense.
It claims that the request includes a body consisting of plain text.
You are making a GET request. There is no body on the request at all.
Advent of code expects you to manually download your input data. It doesn't expect your solution to fetch it from the AoC website.
JS solutions are generally run in Node.js (rather than a web browser) where they can use the fs module to read the local copy of the input file. (Tip: I find it handy to copy/paste the sample data into a sample file to test my results against the worked example on each day).
I am using Chrome and trying to upload a file from browser client. My API given below.
[HttpPost("attachment/{confirmId:long}")]
public async Task<IActionResult> UploadAttachmentAsync(long confirmId, IFormFile filePayload)
{
// ...
}
This works in Swagger but when I am trying from Javascript client, filePayload is null. The confirmId param comes through fine.
I used HttpToolkit and verified that the content of the file is there.
However, this is my client code anyway....
async uploadAttachmentAsync(file, confirmId) {
// buildUrl is my own utility, creates url correctly
const url = buildUrl(`paperConfirm/attachment/${confirmId}`);
const body = new FormData();
body.append('file', file);
// XH is a 3rd party library. I am confident there is no problem there
return await XH.fetchJson({
url,
body,
method: 'POST',
// Note: We must explicitly *not* set Content-Type headers to allow the browser to set the boundary.
// See https://stanko.github.io/uploading-files-using-fetch-multipart-form-data/ for further explanation.
headers: {'Content-Type': null}
});
}
I'm using request module in order to send message to a Telegram bot, everything is fine, except the way I'm display emojis, which don't get printed well
Post request:
request.post({
url: `${this.getApiURL()}/${apiName}`,
formData: payload,
headers: {
'Content-Type': 'application/json',
'Charset': 'utf-8',
}
}, (err, resp, body) => {});
The payload contains a text message with some emojis like:
const _emojis = {
throwingAKiss: '\xF0\x9F\x98\x98',
tearsOfJoy: '\xF0\x9F\x98\x82',
smirkingFace: '\xF0\x9F\x98\x8F'
}
but I'm displaying this symbol ð
As a workaround you may have success with using a different unicode notation, i.e.:
const smiley = "\u{1F604}";
For a list of codes, there are emoji tables out there.
Additionally, it might ease a bit development overhead by using libraries such as node-emoji (which uses raw emojis under the hood through omnidan/node-emoji json file.
I need to call a cloud call in one parse application from the cloud of another parse application through the Rest API.
For this I have this code now. However, I always get the error: com.parse.ParseException: ReferenceError: XMLHttpRequest is not defined at main.js:12:20. (this line is referring to the line where the XMLHttpRequest is initialized). My code for this is:
var url = "https://api.parse.com/1/functions/signUp";
var myObj = new Object();
myObj.age = 45;
myObj.email = "test#gmail.com";
myObj.password = "testpw";
var client = new XMLHttpRequest();
client.open("POST", url);
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.setRequestHeader("X-Parse-Application-Id", "xxx");
client.setRequestHeader("X-Parse-REST-API-Key", "xxx");
client.send(JSON.stringify(myObj));
Since this code is executed in the Parse cloud, I think the problem is that the XMLHttpRequest isn't constructed correctly. Could that be?
Is this even possible with Parse, calling a cloud call from a parse application through the rest-api from the cloud from another parse application?
You can make http calls with Parse.Cloud.httpRequest()
Parse.Cloud.httpRequest({
url: 'https://api.parse.com/...',
headers: {
'Content-Type':'application/json',
'X-Parse-Application-Id':'...',
'X-Parse-REST-API-Key':'...'
},
method: 'POST',
body: JSON.stringify(myObj)
}).then(function(response) {
console.log(response);
}, function(err) {
console.log(err);
});
Give that a shot. re: https://parse.com/docs/cloud_code_guide#networking
You can probably skip the JSON stringify and just pass in myObj.
My code is attempting to post data to a Coldfusion API from my local Node.js server. I have managed to communicate with the API and authenticate myself via the request headers. However I am having difficulty actually passing my JSON object through as I cannot get the structure right.
The API does not accept the JSON option of the request module, so that is my easiest option out of the window.
The API is expecting the following:
{
'source': {
'customer': {
'customerlogin': 'myusername',
'customerpassword': 'mypassword',
}
}
}
my code works if I hard code the following body parameter (from a sucessful post by somebody else) into my post.
var Jrequest = require('request');
var options = {
uri: 'http://myAPI/customerSSO.json',
headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': something', 'Timestamp': timestamp},
method: 'POST',
body: 'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D' // Working
};
Jrequest(options, function (error, response, body){
res.send(body);
});
If I send the JSON through in other ways, for example json.stringify(), it is rejected on the grounds that 'source is required but not defined'.
So I suppose my question is, in node.js how do I turn JSON into something that looks like this
'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D'
or have I overlooked another option?
Thanks for any help and apologies if I have used incorrect terminology.
I think this should work:
var querystring = require('querystring');
...
request({
...
headers : { 'Content-Type': 'application/x-www-form-urlencoded', ... },
body : 'source=' + querystring.escape(JSON.stringify({
'customer': {
'customerlogin': 'myusername',
'customerpassword': 'mypassword',
}
})),
...
}, ...)
Your example also contains newlines and carriage returns and such, but I'm assuming those are optional.