How to send get request with a body? - node.js

I am trying to send a get request to my API to get a list of users. But I need there is an exclude list that the response must exclude. How can I send this exclude list in my GET request?

You can send a body with the request. Query parameters is probably the best way to do it though. The folks at Elastic.co say:
The truth is that RFC 7231—the RFC that deals with HTTP semantics and
content—​does not define what should happen to a GET request with a
body! As a result, some HTTP servers allow it, and some—​especially
caching proxies—​don’t.
The authors of Elasticsearch prefer using GET for a search request
because they feel that it describes the action—​retrieving
information—​better than the POST verb. However, because GET with a
request body is not universally supported, the search API also accepts
POST requests:

You cannot send a request body when making a GET request. However, you can add it as a query parameter. Alternatively, you can make a POST request.

Related

Serving a HTTP request response as a dialog response - Composer Framework

We are developing a chatbot to handle internal and external processes for a local authority. We are trying to display contact information for a particular service from our api endpoint. The HTTP request is successful and delivers, in part, exactly what we want but there's still some unnecessary noise we can't exclude.
We specifically just want the text out of the response ("Response").
Logically, it was thought all we need to do is drill down into ${dialog.api_response.content.Response} but that fails the HTTP request and ${x.content} returns successful but includes Tags, response and the fields within 1.
Is there something simple we've missed using composer to access what we're after or do we need to change the way our endpoint is responding 2? Unfortunately the MS documentation for FrwrkComp is lacking to say the very least.
n.b. The response is currently set up as a (syntactically) SSML response, this is just a test case using an existing resource.
Response in the Emulator
Snippet from FwrkComp
Turns out it was the first thing I tried just syntactically correct. For the case of the code given it was as simple as:
${dialog.api_response.content[0].Response}

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.

Implementing If-Match HTTP header in Express.JS

Context: I am trying to prevent data overwriting due to concurrent updates by multiple users on the same database entity.
Question: How can I implement If-Match HTTP header in POST calls to respond with HTTP status 412 (Precondition Failed) when the DB has already been modified by another user?
My intended approach:
When a POST call is made to POST /user/123, I want to compare the ETag available in If-Match header of the POST call with the ETag present in the response header of the endpoint GET /user/123. To accomplish this, I have to invoke the GET route from the POST route, to extract the ETag. Is this even possible?
You can invoke GET from the POST route using res.redirect('/getroutename')
This way you can compare the ETag received from the get call, and achieve your intended approach.

Send json data (asynchronous) to my response

So,upon request I respond with a basic template(I use ejs) and I want to add json data to it when I get from an external API.
I have noticed that I can't use res.render or res.write twice(I get errors) so I really don't know what to do.Any ideas?
You can only send one response for each request. So you either need to do two requests or combine the data you want to send into one response.

graphiql send as post body instead of query string

On Node I am using express-graphql plugin which provides the GraphiQL UI.
GraphiQL in this implementation always sends the the query as GET querystring.
However, as I understand the GraphQL and respectively the express-graphql documentation, it should also be possible to send queries in the body of a POST request.
Is there any way to get GraphiQL (i.e. the browser IDE) to send the query in the POST body?
Addendum Feb. 26 2017:
I think I found what I needed to know by myself.
For whatever reason I had the query in my querystring. Like this:
[myhostname]?query={users{[myfields]}}
I've overlooked that.
As the express-graphql documentation points out:
If not found in the query-string, it will look in the POST request
body.
This means in turn: if found in query string, it will NOT look in request body.
Therefore it's being treated as a GET request.
So all I needed to do, was to clean my URL from query parameters and the requests were arriving with a POST body.
That was basically all I needed to know.
So it really was a sloppy mistake which I made by not properly watching the URL i.e. not noticing there was a query in the querystring.
GraphiQL is a React component that is fully customizable in terms of how your data is sent to the server (fetcher). The express-graphql middleware basically pulls the transpiled GraphiQL code from a cdn and sends a static html page including it to the client: https://github.com/graphql/express-graphql/blob/master/src/renderGraphiQL.js
With this knowledge you can basically do the same on a separate route, and turn off the built-in GraphiQL from express-graphql
Also please check the GraphiQL docs: https://github.com/graphql/graphiql!

Resources