How to get the query parameters in nock callback - node.js

I want to access the query parameter in nock reply callback.
The request object that is exposed contains the path that has them as a string. But I would like to access them as a map so that I will not have to deal with parsing the string
const scope = nock('http://www.google.com')
.get('/cat-poems')
.reply(function(uri, requestBody) {
console.log('path:', this.req.path)
console.log('headers:', this.req.headers)
// ...
})
I would expect the query params to be a separate map that I can access
Does anyone know of a way to achieve this?

The value of this.req inside a reply function is an instance of a ClientRequest that has been slightly modified.
Unfortunately for your use case, ClientRequest does not provide an easy way to access just the query params. But you do have access to the full path, from which you can parse the query params out.
const nock = require('nock')
const http = require('http')
const url = require('url')
const scope = nock('http://www.google.com')
.get('/cat-poems')
.query(true)
.reply(function(uri, requestBody) {
const parsed = new url.URL(this.req.path, 'http://example.com')
console.log('query params:', parsed.searchParams)
return [200, 'OK']
})
const req = http.get('http://www.google.com/cat-poems?page=12')
// output >> query params: URLSearchParams { 'page' => '12' }
The object being logged is a URLSearchParams instance.
Using the URL constructor is the preferred method over url.parse now, so I've used that for the example. Keep in mind that URL won't parse relative paths alone, it requires an origin, but since you don't care about the host in the end it can be a dummy value (hence the use of "example.com").

Related

JSON as Javascript object in Azure http trigger function

const df = require("durable-functions");
module.exports = async function (context, req) {
const client = df.getClient(context);
context.log(`Function Name = '${req.params.functionName}'.`);
context.log(`Body = '${req.body}'.`);
const instanceId = await client.startNew(req.params.functionName, undefined, req.body);
context.log(`Started orchestration with ID = '${instanceId}'.`);
return client.createCheckStatusResponse(context.bindingData.req, instanceId);
};
I have tried to use POSTMAN or https://reqbin.com/ for testing but I always get object.
It is a simple case but I don't understand why it is not JSON object.
I read this one
TypeScript Azure Function Read Body of POST method as JSON
but it didn't help me.
Everything is fine!
It is already deserialized json object.
It looks like the normal string in a log after
JSON.stringify(req.body)
My fault was that I wrongly got it in the orchestrator function from the context.

How to access query parameters with dots in ExpressJS

Imagine I have the following incoming request with query parameters
https://api.myawesomeapi.com/v1/facebook/group_app_install_hook?hub.mode=subscribe&hub.challenge=1409653872&hub.verify_token=myToken
then I have my express function as below to handle the incoming request above
static async appInstallOnGroupHookHandler(req, res) {
let hubChallenge = req.query["hub.challenge"]; //This is always undefined
console.log(`Hub Challenge ${hubChallenge}`);
let verificationToken = req.query["hub.verify_token"];
console.log(`Hub Verification Token=${verificationToken}`);
return res.status(200).send(hubChallenge ?? 'success');
}
When I tried accessing the hub.challenge query parameter using
let hubChallenge = req.query["hub.challenge"];
hubChallenge keeps coming back as undefined.
Is there a better way to access query parameters that has the dot character in them?
Thanks

How to construct and extract value from simple HTTPS request in Node.js?

I have a simple HTTPS request -
https://api.pro.coinbase.com/products/btc-eur/ticker
In the browser this returns one object. What's the simplest code that will allow me to retrieve and display this object (as is) in the terminal of Node?
const https = require('https')
const url = https.get('https://api.pro.coinbase.com/products/btc-eur/ticker')
const myObject = JSON.parse(url)
console.log(myObject)
A simple copy / paste of the above code in VSC returns the error SyntaxError: Unexpected token o in JSON at position 1.
#mamba76, welcome to the SO community. Please use Node.js node-fetch package. It is much simpler to use. You can install it using npm install.
Following code might help:
"use strict";
const fetch = require('node-fetch')
async function getValue() {
// Invoke the API.
// Wait until data is fetched.
let response = await fetch('https://api.pro.coinbase.com/products/btc-eur/ticker');
let value = await response.json();
return value;
}
getValue().then(result => {console.log(result.price);});
As a good practice, always assume that API calls over the HTTP (whether in your own network or outside) might take time to return data and hence you should use async-await pattern to make these requests.
Extending #Akshay.N's answer and without using external dependencies,
const https = require('https')
https.get("https://api.pro.coinbase.com/products/btc-eur/ticker",res=>{
let body = '';
res.on('data', (chunk) => { body += chunk; });
res.on('end', () => {
const myObject = JSON.parse(body);
console.log(myObject);
})
})
Now, what we're doing here is waiting on the data event as long as the data is coming in, and appending it to the variable body. Once the end event is encountered, we take that as a signal that all data has been received and we can proceed to parse the body into an object using JSON.parse (assuming the data was serialized in JSON; if it wasn't JSON.parse will throw an error).
This tutorial is helpful: https://davidwalsh.name/nodejs-http-request
try something like this:-
https.get("https://api.pro.coinbase.com/products/btc-eur/ticker",res=>{
res.on('data', (chunk) => { console.log(JSON.parse(chunk))})
})
With node (you need request module) :
// display object
(require("request")).get({
url: "myurl",
json: true
}, function(e,r,b){
console.log(b);
});
// display as string
(require("request")).get({
url: "myurl",
json: false
}, function(e,r,b){
console.log(b);
});
With just curl in your terminal (without node)
curl myurl

Append parameters to URL

How can we append the parameters to path URL using axios.get() in react client?
axios.get('/api/order/user/', {
params: {
user_id: 2
}
})
the route defined in the express server is this.
router.route('/user/:user_id').get(//);
This is what return from the above axios.get() code.
GET /api/order/user/?user_id=2
What I want to achieve is something like this.
GET /api/order/user/2
How can this be achieved?
What you want to achieve is path parameter
const url = '/api/order/user/' + user_id;
axios.get(url);
The params property in axios is Query parameter
GET /api/order/user/?user_id=2

Breeze Json uriBuilder

I'm trying to use breeze.js with sails.js.
Therefore I'm using the breeze json uriBuilder.
I logged the req.query and got the following:
{ '{"where":{"name":"foo"},"orderBy":': { '"name"': '' } }
To query waterline objects I need to bring it into a format like this:
{ where: { name: 'foo' }, sort: 'name' }
First thing I tried:
var queryString = JSON.parse(Object.keys(req.query)[0]);
That works as long as I only put a where clause in. But with more parameters i get this strangely formatted json object.
How can I parse it to get the correct object?
Solution:
Don't parse he req.query. Parse the url and parse it. This way u will get a json uery that sails accepts. Now strip of unsupported parameters as select and you're done.
var parsedUrl = urlUtils.parse(req.url, true);
var jsonQueryString = Object.keys(parsedUrl.query)[0];
var jsonQuery = JSON.parse(jsonQueryString);
Good question! We haven't yet documented this adequately, but the basic idea is that there is a breeze-client npm package (https://www.npmjs.com/package/breeze-client) that you can use to parse the incoming json query into a breeze EntityQuery instance, from there you can interrogate the EntityQuery and turn it into whatever server side syntax you want, i.e. in your case Sails.)
// req = the HTTP request object.
var resourceName = req.params.slug; // the HTTP endpoint
var entityQuery = breeze.EntityQuery.fromUrl(req.url, resourceName);
// you would write the transform to sails below.
var sailsQuery = tranformToSails(entityQuery);
This is exactly what we do in the breeze-sequelize npm package where we take the incoming req.query and go thru the process above to create a 'Sequelize' query.
See http://www.getbreezenow.com/sequelize-mysqlpostgressql-lite

Resources