2

I'm trying to do an Elasticsearch GET query with a very simple script using Postman. When the script is all on one line it works but if I try to do multiple lines I get an error

I'm sening the data as JSON with Content-Type: application/json in the header

Example - Works:

{
    "query":{
        "match_all": {}
    },
    "script_fields": {
        "my_custom_field":{
            "script": {
                "lang": "painless",
                "source": "int count = 1; return count;"
            }
        }
    }
}

Example - Produces Error:

{
    "query":{
        "match_all": {}
    },
    "script_fields": {
        "my_custom_field":{
            "script": {
                "lang": "painless",
                "source": """
                   int count = 1;
                   return count;
                """
            }
        }
    }
}

The error:

 "Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@20493763; line: 9, column: 18]"

I think postman may be adding line breaks in behind the scenes.

Valentina
  • 518
  • 7
  • 18
Thomas Kagan
  • 440
  • 3
  • 16

1 Answers1

3

Triple-quotes in JSON are technically not valid -- see this thread for more info.

You've essentially got 3 options:

  • Write a script which takes in a multiline "JSON" text and produces a \n-separated, valid JSON (what I often did before multiline backtick strings were a thing in JavaScript, and still do in php):
function compactifyMultilineString( $input_string )
{
    return str_replace( array( "\r", "\n", "\t" ), " ", $input_string );
}
  • Use postman's own pre-request scripts
  • Or, probably the most reasonable option, set up Kibana right next to your ElasticSearch server. Kibana is great for testing out queries and it also supports a postman-ready copy feature:

enter image description here

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
  • 1st) Your first solution says write a function to parse out linebreaks but where would I be executing that function? Is it somwhere in postman? The point of my question is to be able to write multiline scripts in Postman. 2nd) I looked into Postman's pre-request scripts however this feature doesn't allow you to change the request body, which is what I would need to change because that is where my script is. 3rd) Thanks for this suggestion however I don't have access to Kibana and this seems like more of a work around than a solution that allows me to run multi-line scripts in postman – Thomas Kagan May 22 '20 at 18:24
  • Change Postman request body - https://github.com/postmanlabs/postman-app-support/issues/4808 – Thomas Kagan May 22 '20 at 18:24
  • Then the answer is no. You cannot do that in postman. The investment to get Kibana going is worth it though. What do you use Postman for? JSON payloads to `_search` and other endpoints, no? All of that works in Kibana, plus multiline triple-quote strings, plus autocomplete and much more. Give it a shot. – Joe - GMapsBook.com May 22 '20 at 20:48
  • Just stumbled on this: https://stackoverflow.com/a/55954798/8160318 – Joe - GMapsBook.com Jun 04 '20 at 08:25