POST Method in Feathers Example - node.js

Can someone explain, how I make a POST method using Feathers and test it in postman. I notice that there are two parameters, "data" and "params". What are their differences? Can someone give me a complete example how to create POST method in feathers and test it in postman?
Thanks

The data is the actual data passed to the service method, ex: a form data. and the params contains the provider (i.e REST, Socket.io or Primus), connection details, authenticated user details and other info related to that service.
For post method you can use the create(data, params) method of the service that you are calling and do your post activity there like creating records like below.
app.use('/messages', {
messages: [],
create(data, params) {
this.messages.push(data);
// Your post activity here
return Promise.resolve(data);
}
});
And in postman use can use the URL http://localhost:3030/messages and in the request body provide the JSON you want to pass as a data to the POST method
ref: https://docs.feathersjs.com/api/services.html

Related

Coingate callback post method data not seen

I am trying to integrate crypto pay with coingate to my nodejs app but the callback post method has a req.body which is empty. Please do anyone have any idea on how to retrieve the order data so that I can use it for another api call. I have looked on the entire req object and couldn't see the order data.

How to access json response data using Axios, node/express backend

I have this project I’m working on and I am using node/express + Axios to retrieve data from a third-party API.
I am attaching an image of the response I am getting from my postman but,
I am having an issue figuring out a way to access and manipulate a specific set of data.
If there are any resources anyone could share that would help I would appreciate it.
as of now I just have:
axios.get('apiUrl')
.then((response) => {
const cardData = response.data;
res.send(cardData);
}
This is the response I get:
for example, I’d like to access the “abilities” property.
Since that property is within the “0" object within the response object, I’m a bit confused as to how to navigate this in the code.
I’ve tried response.data.0 but that doesn’t seem to work.
function retrieve(callback){
//I don't get why you are using request.send here. Are you routing the response elsewhere?
//If you are just consuming a service, use Axios with a callback instead.
//If you're not routing it you won't need Express.
axios.get('apiUrl').then(response => callback(response));
}
function clbk(response){
let obj = JSON.parse(response); //In case you are receiving a stringified JSON
//Do whatever you want with the data
//If you have a number as a key, access it by using []. So, considering your example:
response.data[0]
}
//CALL:
retrieve(clbk);

NodeJs with Express not parsing form data from node-fetch

I'm creating two APIs with NodeJS, Express, and TypeScript. One API will take a request with a content type of multipart/form-data, and will use the body in the request to make another request to a second API.
I'm using Postman, so the chain of request looks something like this
Postman -> First API -> Second API
I use node-fetch to make a request from the first API to the second one. The body of the request is a FormData, which contains some files and key-value pairs.
const form = new FormData();
// File for profile picture
const profilePictureBuffer = await (await fetch(user.profilePicture)).buffer();
form.append('profilePicture', profilePictureBuffer);
// File for ID Card
const idCardBuffer = await (await fetch(user.idCardUrl)).buffer();
form.append('idCard', idCardBuffer);
// This part iterats over the obsect of 'user',
// which contains other key-value pairs
Object.entries(user).forEach((data) => {
form.append(data[0], data[1]);
});
// Make POST request to second API
const pinterUser = await fetch(secondAPIURL, {
method: 'post',
body: form,
headers: form.getHeaders()
});
I ran both of the APIs on localhost so that I can monitor the logs for any bugs. As I make a request from Postman to the first API, then the first API make another request to the second API, I got the following error log in the terminal for the second API
TypeError: Cannot read property '0' of undefined
After some investigation, I found out that, in the second API, the req.body and req.files are empty objects. This means that Express did not parse the incoming request. Note that I've also already a multer middleware to handle the files in the request.
Furthermore, I have added the following lines of code in my server.ts file for the second API
/** Parse the body of the request */
router.use(express.urlencoded({ extended: true }));
router.use(express.json());
However, when I tried making the request from Postman, it returns a successful response.
I'm not really sure what's going on here. I've tried looking for some answer regarding this similar issue, but most of them suggest adding urlencoded and json, or using some other library to handle parsing form data.
In my case, the first suggestion doesn't solve my problem since I already added them from the start, and the latter is what I'm trying to avoid.
I wonder if anybody could point out what I was missing here? Thanks in advance

Using graphene, when sending a graphql request with a client, how can I intercept + introspect post data?

Using graphene, when sending an outbound graphql request with a client, how can I intercept + introspect query params and post data?
This request is going from one backend service to another backend service
Both queries an mutations are sent via POST
My use case is that I need to cryptographically sign a data payload(dict) but to do that I need graphene to go through the steps of converting the query int a GET or the mutation into a POST before I can grab the data from the query params or post body.
The input data needed to sign the request is the dict:
data = {
"operationName": "blah",
"variables": {},
"query": "fancy graphql query here"
}
This request data:
is available when calling client.execute
is NOT fully available in the graphene middleware because we do not have access to the string value of the query in the resolve info (at least in the test client)
So the only place that one can access these inputs is right before client.execute is called or by making a custom version of the Client class, that implements an execute method, bake the signing in there, then call super().execute...

$resource.query() parameters fail to arrive at server

Trying to implement a RESTful API using Node.js and Angular.
On the client side, I define a resource called songResource with the $resource function in Angular.
I then call the API with this method:
$scope.displayArray = songResource.query(
parameters, // an object containing various settings
function(value, responseHeaders) { // success callback
console.log('Success!',parameters);
...
},
function(httpResponse) { // error callback
console.log('Error!');
...
}
);
On the server side, the route resolves, but the parameters do not come over. (In other words, req.params is an
empty object.) The rest of the service retrieves and sends zero records. My client receives the zero records and
hits the success callback, where I can tell that the API call succeeded.
Why aren't the parameters coming over to the API, and how do I fix this?
Found out what I was doing wrong. My parameters come in through the query string. Therefore, I need to use req.query rather than req.params to get the parameters.

Resources