Workflow - REST - Response Header

Published on Friday, 9 February 2018

A question from @epkpej was asked on another of my Articles

Using the REST Generator (Response Content) in Workflow 7.6
https://www.symantec.com/connect/articles/using-rest-generator-response-content-workflow-76

They wanted to get the "Response Header" from a POST Request.

Unfortunately this isn't possible using the REST Generator. It isn't exposed in the components that are generated by the tool.

Please vote on the Idea/Feature Request:

As an alternative you can swap to a Code (Script) Component

Now we need to add a couple of Inputs

  1. Inputs
  • URL - Text
  • HeaderValue - Text
  1. Result Variable
  • Result Variable Type - Text
  • Result Variable Name - Header
  1. Source Code

Namespaces

  • System.Net
  • System.IO

Now for the code

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/json";

using (WebResponse response = request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        //responseString = reader.ReadToEnd();
        string header = response.Headers[HeaderValue];
        return header;
    }
}

As you can see the URL is being used in the first line

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

And the Header in a few down

string header = response.Headers[HeaderValue];

This makes the code a little more resuable but there is lots more you can do.

  1. Test Page

Now we need an API to test

https://swapi.co/

If we test it in Postman

https://swapi.co/api/people/1

There are a number we can chose from, lets use Etag.

Etag 145c70f4eca80b4752674d42e5bf1bcf

Now you might wish to return the collection of headers and loop through them and find the value you wish either in code or with other WF components.


If you do need to work with REST the Generator should cover most scenarios and I've written a set of Articles:

Workflow - REST Generator
https://www.symantec.com/connect/articles/workflow-rest-generator