Only return the response status from a axios GET request? - node.js

I`m writing tests in playwright where I check if all the links on my web page are valid.
I'm using Axios to get the responses and because I have a lot of links I want to expedite the tests as much as possible, so I want to receive only the status response code from Axios without the response data, is this in any way possible with Axios or maybe to use another library to achieve this?

I used axios.head() request, which only returns the response header and not the data, but had to add to use:
axios options: decompress: false// default true
decompress indicates whether or not the response body should be
decompressed automatically. If set to true will also remove the
'content-encoding' header from the responses objects of all
decompressed responses

Related

Why do we need to add .end() to a response?

Currently building a RESTful API with express on my web server, and some routes like the delete route for a document with mongoose ex. await Note.findByIdAndRemove(request.params.id) response.status(204).end() send response statuses with end()
Why do I need to add the .end()? What in these cases, and why cant one just send response.status(204)
With some responses that return json, the response.status(201).json works fine
Only certain methods with Express or the http interface will send the response. Some methods such as .status() or .append() or .cookie() only set state on the outgoing response that will be used when the response is actually sent - they don't actually send the response itself. So, when using those methods, you have to follow them with some method that actually sends the response such as .end().
In your specific example of:
response.status(204)
You can use the Express version that actually sends the response:
response.sendStatus(204)
If you choose to use .status() instead, then from the Express documentation, you have to follow it with some other method that causes the response to be sent. Here are examples from the Express documentation for .status():
res.status(403).end()
res.status(400).send('Bad Request')
res.status(404).sendFile('/absolute/path/to/404.png')
Since all three of these other methods will cause the response to be sent and when the response goes out, it will pick up the previously set status.

grcp request payload format

I'm trying to log in into a site which requires grcp content-type using requests. I alrady have a HTTP 2 client, but I don't know how body of my post request should look like.
When I'm trying to simply copy request as a curl from chrome network tab, request body looks like this:
%äEMAIL"PASSWORD(0
When I'm trying to request site with same body as I copied from chrome tab, I'mm getting response with this headers:
Grpc-Message: grpc: received message larger than max (218767392 vs. 4194304)
Grpc-Status: 8
I'm sure It's becouse wrong payload format
If anybody knows how can I pass data in request plase help.
If you're trying to send a one-off gRPC request, https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md would be helpful to know as to how to construct a message. Otherwise, using gRPC clients (https://github.com/grpc/grpc) would make more sense.

How to send a http response using koajs

I'm trying to validate a webhook via facebook. So facebook hits my url my-url/facebook/receive within my route in nodejs i'd do res.send(req.query['hub.challenge']); to send an http response.
I'm using KoaJS. From what i understand, Koajs merges the request and response object into ctx but when reading through the docs I can't find anything along the lines of ctx.send or similar to send a http response.
Can anyone give me some direction or links.
Thanks.
To send the body of a response, you can simply do ctx.response.body = 'Hello'. There are many aliases attached to ctx, so you don't necessarily have to reference the response or request yourself. Doing ctx.body = 'Hello' would be the same as the code above.
If you wanted to set headers, you would use the ctx.set() method. For example: ctx.set('Content-Type', 'text/plain').
To access the query parameters, you would use ctx.request.query['some-key'] (or simply the alias ctx.query['some-key']).
All of the different request/response methods are documented pretty well at the Koa website along with a list of aliases attached to ctx. I highly recommend you give it a read.

Accessing request body with ZombieJS

I'm using ZombieJS v4 (4.0.13), and am trying to access the client's request body, since some tests require me to monitor the fields that are automatically sent via client forms.
While I can access all the query-string fields by using the pipeline's addHandler hook, I can't seem to read the request body in order to extract body post params.
Working with the response body is easy - I can access it from the pipeline by waiting for the response to consume. But the request body always appears as null, with _bodyUsed = true, so I can't wait for it in the same manner.
Anyone knows of a way I can access the sent request body, either via the pipeline, or somewhere else?
10x

Node Express parses request body with JSON incorrectly

I am sending a JSON object from web client, which looks like this:
{"AudioEncoder":{"Settings":{"1":{"audio_bitrate":"16000"}}}}
And in the request I get from req.body.myvalue:
{"AudioEncoder":{"Settings":[null,{"audio_bitrate":"16000"}]}}
In the Network panel of my browser I see correct value though:
myvalue[AudioEncoder][Settings][1][audio_bitrate]:16000
Error is where I am expecting object with key {1:... but get [null:....
Any ideas why would this happen?
I suspect your browser isn't actually sending JSON, it's sending application/x-www-form-urlencoded. This is not the correct value if you are trying to have the browser send JSON: myvalue[AudioEncoder][Settings][1][audio_bitrate]:16000. That's not JSON. Check the request headers for Content-Type and look at the raw body of the request to verify this. If you post your browser JS that's sending the AJAX, we can help you fix that. jQuery makes it a little tricky to specify the options correctly to get it to really send JSON.

Resources