How to create a cookie when making an axios request? - node.js

I'm building an app for personal use that gets data from Walmart and then displays clean data depending on what I want. I am having a problem. Every day, when I run my app. I have to insert a new cookie inside the headers because I guess it changes every day? Not sure but I was wondering if there is a way to create a cookie automatically or something like that.
I am using axios to make my get request. I already tried using npm cookie-parser, withCredentials: true, and a bunch of other things and none of them work. When I try it, the get request just fails. The only way it works is when I manually send a request, steal that cookie and put it inside my app.
Thank you.

Related

How can I proxy a GraphQL request from my Node.js app in an efficient way?

My current setup for the project I'm working on is:
Next.js
Wordpress backend with GraphQL plugin enabled
They live on two different servers
I would like to be able to make a request from a Next.js page that proxy via an api-route to the Wordpress backend. I want the GraphQL query to be passed along and I would like to be able to modify the request (for example add header, set a cookie etc) before it reaches the Wordpress backend.
I first tried to achieve this using this module: https://github.com/http-party/node-http-proxy and using the .web() request. It almost worked except I got back a response from Wordpress that I wasn't able to decode (tried with Buffer etc, but no success).
So my current way to do this is to make an axios-request from my api route and pass along the req.body in that request, and that setup works.
However, is this way to proxy OK or should I try to make it work with node-http-proxy? Don't know about what possible benefits there are.
Thank you
if you use Axios you will make an extra request when you retrieve data from the source. This will decrease performance. On the other side if you use proxy you will forward the incoming request and this way you will have improved performance.

QR code best approach for POST request from REST API

I'm setting up a website that will be mobile focused and one of the features I wan't to implement is users to be able to log an entry by just scanning a QR code.
For what I read is not really possible to make a POST request directly from a QR code, so I was thinking in two different options:
1. Make a GET request and then redirect that inside my server to a POST route in my routes.
So the URL would be something like https://example.com/user/resources/someresourceid123/logs/new and then this would create a POST request to https://example.com/user/resources/someresourceid123/logs/ and create the new entry to then send a response to the user but I'm not really sure this is the best approach or if it's possible at all.
My POST request only requires the resourceid which I should be able to get from req.params and the userid which I get from my req.user.
2. Do my logic and log the entry to my DB using the GET request to https://example.com/user/resources/someresourceid123/logs/new.
This would mean that my controller for that request will do everything needed from the GET request without having to make an additional POST request afterwards. I should be able to get both the resourceid and userid from the req object but not sure if being a GET request limits what I can do with it.
If any of those are possible, which would be the best approach?
I'd propose to go with a second option simply for the sake of performance. But you need to make sure your requests are not cached by any proxy, which is usually the case with GET requests.

Make Python script open browser and execute HTTP post request

I want to send some HTTP POST requests to a website. There's no problem with doing it by my browser, since I'm logged in and I have my authenticated session.
I don't know how to make POST requests by Python while using that authenticated session (so importing data from the browser), and I don't even know if it's possibile, so I thought that I could trigger these POST requests in browser from the Python script.
I know there exists a library called "webbrowser" that allows to make requests by opening new tabs in browser. My problem is that I need a POST request, not a GET one.
So I ask you for 2 suggestions: is there a way to import authenticated session such that Pyhon can make requests for websites that require auth?
If not, is there a way of doing something like this:
webbrowser.open(url, new=0)
but with POST requests?
Thanks!
I agree with Mr. C, but alternatively...
Checkout Selenium. You can't make a direct POST request, but you can do anything that you would usually do as a normal user.
Check out the requests library. Seems like you need more functionality than the webbrowser module provides.

Make sure nodejs express route can only be called once, until finished

My endpoint
POST example.com/user/transfer/
should not be able to be called by the same user until his earlier request has been processed. How do I do that?
Ideas
Keep a request Nonce in the user table (database) and make sure it can only be used once.
?
I might be not wrapping my head around it the right way.

Cannot request Iron Router server route twice without refreshing the client

I am calling a meteor method, which generates a file using fs. I wait for a callback giving me the path where the file exists, and then I request the file with a server route. The code is very similar to this SO answer. I have also tried using createReadStream (as demonstrated here) instead of passing the file directly to response.write.
This all works well on the client's first click of my export/download button. However, if for some reason they want to click the button more than once, the file will get generated but the file will not get served by Iron Router. There are no errors on the client or server. If the user refreshes the client, then the feature will work again (once).
Why do I need to refresh the browser in order to request the same server route a second time? Am I doing something wrong?
Example Application
Does the URL change when they click the first download? If so, and the second route is the same, you will not get redirected as you are already there. If this is the case, can you use the router hooks to send the user back to the route they came from?

Resources