How can I send an conditional GET method to elasticearch from postman? - rest

I have a GET request with it's query body as shown below:
GET /courses/_search
{
"query": {
"bool": {
"must": [
{"match": {"teacher.keyword": "Andrew Ng"}}
]
}
}
}
In Postman, GET requests can not have a 'body'. so how can I send this request to the elasticsearch? How should I specify this neseted structure of the body in the request?

Use POST instead 😃
Elasticsearch is a bit more permissive and supports both GET and POST to send a request with a payload.

Related

Proxy request to graphql server through AWS appsync

I have a graphql server on one of my EC2 instances running. I also have AWS appsync running, but at the moment it's integrated only with couple of lambdas.
I'd like to connect my Appsync with graphql server, so Appsync will behave as a proxy for specific queries/mutations.
So from client side, it will look like this:
Client sends a query to the appsync, lets say that it looks like that:
{
user {
id
}
}
Appsync has defined a user query, it's configured to proxy the query the graphql server, without any changes
Graphql server is able to handle following query:
{
user {
id
}
}
and returns a result:
"data": {
"user": {
"id": "123456789"
}
}
finally Appsync proxies response back to the client
Am I able to configure Appsync in a way that given scenario is possible? Is it the right pattern to use Appsync for?
Update 1. After #mparis response
I was able to proxy my request through AppSync, and reach my graphql server, with following resolver configuration:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/graphql",
"params":{
"body": {
"query": "$util.escapeJavaScript($ctx.info.getSelectionSetGraphQL())"
}
}
}
But this still does not work as expected - it's different from description in docs, and I see at least two issues:
If I have a query with arguments and nested paylod, $ctx.info.getSelectionSetGraphQL() cuts out the query name and arguments part, and gives me only nested payload, so this query:
{
user(id: "1") {
picture {
url
}
}
becomes following one, once I call $ctx.info.getSelectionSetGraphQL():
{
picture {
url
}
}
But I'd like to use whole query, same as described in docs:
"selectionSetGraphQL": "{\n getPost(id: $postId) {\n postId\n title\n secondTitle: title\n content\n author(id: $authorId) {\n authorId\n name\n }\n secondAuthor(id: \"789\") {\n authorId\n }\n ... on Post {\n inlineFrag: comments {\n id\n }\n }\n ... postFrag\n }\n}"
Lets say I have a query, and I've defined resolver for user query in appsync, the query looks like this:
{
user(id: "1") {
picture {
url
}
}
and I call my graphql backend through appsync, in my graphql logs I see following response logged:
{
"data: {
"user": {
"picture": {
"url": "example.com/link/to/a/picture"
}
}
}
}
but appsync returns me given response instead:
{
"data: {
"user": {
"picture": null
}
}
}
I've figured out, that appsync expects from me that I will define resolvers for both user and picture query. Appsync wants to first call user and then picture query, but what I wanna do is simply proxy the response through appsync, without calling any additional queries except user. Everything is evaluated in my graphql backend and should be just put to the response body, but appsync wants to evaluate everything one more time.
Is there any solution for no 1. and 2.?
As of writing, you can use the $ctx.info object to get the selection set of queries from within the resolvers and send the relevant data via an HTTP resolver to your downstream service. Go here and look for the info field on the $ctx object. To make this work, you will need to mirror the schema of your downstream API within your AppSync API.
Thanks for bringing this up. The team is aware of these use cases and this helps prioritization.
You can use the following resolver function:
I used the openly available StarWarsAPI as data source: https://swapi-graphql.netlify.app
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/.netlify/functions/index",
"params": {
"headers": {
"Content-Type": "application/json"
},
"body": {
"query": "query { starship (id: \"$ctx.args.id\") $util.escapeJavaScript($ctx.info.getSelectionSetGraphQL()) }"
}
}
}
And then you have to get the data from the graphql response using this reponse mapping:
$util.toJson($util.parseJson($ctx.result.body).data.starship)
(You have to duplicate the schema in appsync)

ServiceNow em_event table additional_Info field is returning [object Object]

We are using Rest API via PowerShell (Invoke-RestMethod), in order to insert records in ServiceNow event [em_event] table with a single call, using the web service API.
We successfully inserting events to the em_event table,
but the only problem is with the additional_info field.
For some reason,
The JSON structure of my PowerShell script,
Is causing the output of additional_info, to return as an Object and Not as JSON string.
And as a result,
The values in additional_info not showing properly, but instead as [object Object]:
This is the JSON structure in my PowerShell script:
# Specify request body
$body = #"
{ "records":
[
{
"source":"MyClass",
"event_class":"$AtargetResourceType",
"resource":"$AtargetResourceType",
"node":"$AtargetResourceName",
"metric_name":"$Aname",
"type":"$AsignalType",
"severity":"$Aseverity",
"message_key":"$Aid",
"u_mc_object":"$AtargetResource",
"description":"$Adescription",
"additional_info":"{
'u_mc_object_class':'$AsourceCreatedId',
'u_mc_parameter':'$AmetricName',
'u_mc_parameter_value':'$AmetricValue'
}"
}
]
}
"#
image which you have posted is not opening. but according to your issue below line will return string value for additional_info:
($body|ConvertFrom-Json).records.additional_info
I had the same issue sending the request using Postman, I was sending the request like this:
{ "records":
[
{
"source":"BMC TrueSight",
"type":"Incident from trusight",
"severity":"1",
"description":"This is a test from WEB SERVICE API ERROR",
"additional_info":{
"status":"new",
"description":"This is a descriotion from additional information",
"category":"41",
"subcategory": "test",
"company": "test",
"business_service":"test"
}
}
]
}
and it showed [object][object] in the additional information field, what I did was sent that field as a string like this:
"additional_info": "{\"assignee_group\":\"ETSS\/UNIX99\",\"status\":\"new\",\"description\":\"This is a descriotion from additional information\",\"category\":\"41\",\"subcategory\":\"test\",\"company\":\"test\",\"business_service\":\"
}
You only need to convert the JSON into a string.

URL parameter vs post form data

URL parameter or post form data; which is more computationally efficient for a backend API to query?
example:
GET : user/:id
POST: user/ { "id": "some_id" }

JSON server - Is it possible to update an object id?

Here is my db.json :
{
"users": [
{
"id": "1"
"name": "John"
}
]
}
I'd like to be able to update the user id by sending a PUT request on the existing user. But the following does not work:
Request URL :
PUT /users/1
with body:
{
"id": "2"
"name": "John"
}
Is there a way to update an object id?
If you are using PUT request means ,the request URL should be like this "PUT/users/1" .
Refer below mentioned image.
I'm using postman to send put request
This does not seem to be possible, as said in documentation:
Id values are not mutable. Any id value in the body of your PUT or
PATCH request will be ignored. Only a value set in a POST request will
be respected, but only if not already taken.

Can't post node that requires a pre assigned value with services api

I have setup a content type with a subject field that has pre assigned values in a dropdown field.
I am using the services api to post new content from a polymer app.
When I POST to the api I send the field structure and value in json but get and error.
"406 (Not Acceptable : An illegal choice has been detected. Please contact the site administrator.)"
Even though the object I am sending matches one of the required values in the field.
Do I need to prefix the value with something? I assume I'm posting to the right place to get that response but don't know why it would accept anything other than the string value.
Here is what I sent to the api which is picked up by my Charles proxy.
{
"node": {
"type": "case",
"title": "my case",
"language": "und",
"field_subject": {
"und": {
"0": {
"value": "subject1"
}
}
},
"body": {
"und": {
"0": {
"value": "my details of subject"
}
}
}
}
}
And here is an example of what I have setup in my Drupal field
subject1| first
subject2| second
subject3| third
subject4| forth
For anyone else with the same problem, this subject is poorly documented, but the answer is simple, my subject did not need the value key despite devel suggesting thats how it would be formatted.
"field_subject": {
"und": [
"subject1"
]
}
I could also shorten my code with "und" being an array.