2

I'm calling a web api that returns a multi part HTTP response.

In the form of

...possible other content...

--97b6f48d-7f7e-4e96-bd0b-ced047222460
Content-Type: application/json; charset=utf-8

{"key":"this is the JSON"}
--97b6f48d-7f7e-4e96-bd0b-ced047222460--

...possible other content...

I want just the JSON: {"key":"this is the JSON"}.

The GUID 97b6f48d-7f7e-4e96-bd0b-ced047222460 is a boundary identifier, identifying that part of the http response, randomly generated.

I know that there is only one part of the response that has JSON content. What is the best way for me to extract it?

FYI I'm using scripting in postman, so it will need to be javascript.

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • Looks quite similar to this question: http://stackoverflow.com/q/21229418/215552, but I don't know how Postman does its scripting. BTW, you might want to lead with that, rather than burying it. – Heretic Monkey May 09 '17 at 21:33
  • Can you use regex on postman scripting? If yes then I think that would be the best way to get an object inside a string. – Yousef_Shamshoum May 09 '17 at 21:35
  • @Yousef_Shamshoum I considered this but I do not think regex is the appropriate solution given the recursive structure of JSON. The best thing to do if I'm going to manually parse it, would be to scan for first and last bracket. (given that I know there is only one JSON document in the response) – James Wierzba May 09 '17 at 21:39
  • Can you provide a URL which returns your multipart http response? Can't you just split the `responseBody` with the GUID boundary and then parse JSON? – shaochuancs May 10 '17 at 02:41
  • @shaochuancs I cannot provide a URL. Yes that is my default plan, to parse the response to find the JSON. It is simple to do but just messy. I was hoping there was some standard way to split the responses in postman, given that it is a HTTP standard – James Wierzba May 10 '17 at 22:46

1 Answers1

2

I haven't found any automatic way to split the multi part HTTP response in postman so I've resorted to parsing the entire response and finding the {...} JSON

Since I know for a certainty that there is only one JSON document object inside of the entire multi-part response, I can look for the outer-most brackets.

It utilizes a counter variable to keep track of the opening/closing brackets. When the counter hits zero (after finding an opening bracket, which sets the counter to 1), we know that we have found the end of the JSON.

This is the code:

//find the JSON in the multi part response
var p, q;
var foundFirstBracket = false;
var stackCount = 0;
for(var i = 0; i < responseBody.length; i++)
{
    if(responseBody[i] === '{')
    {
        if(!foundFirstBracket) 
        {
            foundFirstBracket = true;
            p = i;
        }
        stackCount++;
    }
    else if(responseBody[i] === '}') 
    {
        stackCount--;
    }
    if(stackCount === 0 && foundFirstBracket)
    {
        q = i;
        break;
    }
}
var jsonString = responseBody.substring(p, q+1);
var jsonData = JSON.parse(jsonString);
James Wierzba
  • 16,176
  • 14
  • 79
  • 120