spring-integration: how to pass post request parameters to http-outbound - spring-integration

I need to pass a post request parameters to an int-http:outbound-gateway and can't find any simple way to do this. What I have to do is issue a http request to a couch db fetching multiple documents with a single request as described in the couch db documentation:
curl -d '{"keys":["bar","baz"]}' -X POST http://127.0.0.1:5984/foo/_all_docs?include_docs=true
So how do I pass the param with the name 'keys' to the int-http:outbound-gateway? (I have all the data I need in the payload of the message)
<int-http:outbound-gateway request-channel="requestChannel"
url="http://127.0.0.1:5984/foo/_all_docs?include_docs=true"
http-method="POST"
expected-response-type="java.lang.String"/>

The payload of the inbound message becomes the body of the POST; selected headers are mapped and you can control that with a number of header mapping options; see the documentation.
If you need to transform some java object to the JSON above, you can add a transformer upstream of the gateway, or configure a custom MessageConverter into the gateway itself.
Your curl command is sending the JSON as the body of the POST; the gateway will do the same thing.

Related

Nest.js API endpoint for validating the POST data

I want to validate the entity data separately from making a POST request and display errors in the form. I'm using class-transformer. What's the best way to achieve this with Nest?
Sounds exactly like what the ValidationPipe is for. You decorate your DTO with class-validator decorators, set the #Body() type in your endpoint, and bind the pipe and viola, the request gets validated when sent to the server and returns errors that you can parse if any exist

How to run a SOAP request in NodeJs?

It might sound like a repeated question at first, but I've gone through all the blogs/ tutorials/ videos I found but none of them actually says how you run that request. Example: for a RESTful request, you code in NodeJs, hit the route(https://localhost/3000/api/getStudent) and get the response. In code you use router.post('/getStudent', async (req,res) => {
// Check response here or manipulate
})
But in soap after coding all the functions like in here-
https://medium.com/metrosystemsro/with-node-js-wrap-backend-soap-webservices-in-a-restful-api-a96887575046
https://dafabulousteach.wpcomstaging.com/2016/05/19/making-a-soap-call-with-node/
https://betterprogramming.pub/how-to-perform-soap-requests-with-node-js-4a9627070eb6
Where do I call the the function and how do I pass parameters and test it? And how do I check the response?
To place this in the context of what you are asking about, a SOAP service is just a server listening on an address for POST requests that have an XML payload. That's it.
So if your SOAP web service address is http://example.com/service_endpoint then you can call the web service by making a POST HTTP request at this address and sending it a SOAP XML message as payload.
Obviously, the XML message in the request must match something that the service expects and you know how to build that XML either by reading documentation or by using the WSDL of the SOAP web service.
So if you know how to make a POST HTTP request to a REST service address and send it a XML payload (although most commonly for REST you use JSON), you already know how to call a SOAP web service.
Now, for convenience, because SOAP is a protocol, the way you call the service and what it expects as XML payload is described by the WSDL, and you can use tools that read the WSDL and create a client API that you can invoke like any other method in your code. The tools handle the details of the HTTP POST request for you, and also the marshalling of any parameters to XML. This is probably what you found confusing.
So let's take an example.
If you have, say, a service that has an operation named savePerson which accepts firstName and lastName as parameters and is described in the WSDL, then your tooling might generate a client that has such a method and accepts a Person object and you can call it like:
var response = client.savePerson({ "firstName": "Kim", "lastName": "Seokjin" });
or some variation of this. You then get a response that you can read just like any other object. You might get a promise instead, or an event, or whatever way the client chooses for how to work. When you use this code, what happens is that the client performs a HTTP POST request behind the scenes for you and marshals the person object to XML, something like this:
POST /service_endpoint
Host: http://example.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<savePerson>
<person>
<firstName>Kim</firstName>
<lastName>Seokjin</lastName>
</person>
</savePerson>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
You can obviously send this request yourself with whatever HTTP library you prefer if you don't want to generate a client from the WSDL. But people prefer to use a generated client instead of dealing with these details directly (i.e. making HTTP requests, parsing XML, etc).

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...

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.

ContentType is null when making Get requests using ServiceStack JsonServiceClient

In my integration tests, ContentType is null when I make Get requests using ServiceStack's JsonServiceClient. However all the Post requests have a ContentType. Is it excluded on purpose? ie. Do I need a content type for http get requests?
Is there a way I can set the ContentType using this client?
There is no Content-Type for GET Requests since it doesn't have a Request body.
The Content-Type on the Request Header specifies what format the Request Body is in, likewise when on the Response Body specifies what format the Response Body is in.
You may instead be after the Accept HTTP Request Header which tells the server the preferred Content Types it would like the Response in.

Resources