How to store response headers from a web service Task in Actions ? | XM Community
Solved

How to store response headers from a web service Task in Actions ?

  • 15 September 2020
  • 7 replies
  • 130 views

Hi guys,
I am trying to call an ODATA service as web service Task in Actions tab to create a ticket in SAP C4C for negative response.
Is there anyway to store the response headers from the GET call of ODATA Service because I want the x-csrf-token (prsent in the header response) to do the POST Call with the same ODATA service and send ticket body.

icon

Best answer by niels_26 3 January 2022, 14:33

View original

7 replies

Badge

Dear shruthi,
hope you have fixed your Issue already, for all others which have the same issue here might be a solution for it:

  1. Use Web Service Block (New Task: Web Service)

  2. Use Code Block (New Task: Code)

  3. Insert following coding:

function codeTask() {
let headers = ~{ch://OKAC_12i3abcJnKMx/headers};
return {
"headerVariableOutput": headers['headerField']
}
}
E.g. "headerVariableOutput" is the variable you can use for further requests in the action workflow. 'headerField' is your received response header which can be e.g. x-csrf-token or any other ApiToken or header field.
"~{ch://OKAC_12i3abcJnKMx/headers}" can be added per piped-text function to insert the dynamic header from the 1. web service request/response.
Hope this is helpful for anyone. BR.

Badge +3

MaHuMhp, I tried your proposal but the response seems empty (SyntaxError: Unexpected token return). Also tried to send the complete header by an e-mail task but also with an empty result.
When I run the fetch token request on Postman it works fine. Do you have another proposal or idea why the response is empty?
Many thanks in advance.

Badge

Dear niels_26,
we have the following sample code in place in our implementations for oData service integration.
function codeTask() {
var headers = ~{ch://OCAC_3aYYb1c2deL3D4O/headers};
var headersSetCookies = headers["set-cookie"]
   var sessionId;

  headersSetCookies.forEach(function(headerValue) { 
     if (headerValue.indexOf("SAP_SESSIONID") === 0) 
     sessionId = headerValue; 
    } 
  );        
return {
"xcsrftoken": headers['x-csrf-token'],
"eTag": headers.etag,
     "sessionIdCookie": sessionId
}
}
You could do some debugging to comment all code lines which are running into errors and add as much variable you want to see and check the values to the return statement (as you would do with js console.log() statement)
Hope this is helping you.
BR

Badge +3

MaHuMhp , thanks for your quick reply.
Unfortunately no luck so far, the result is still empty, therefore 2 questions:

  • Did I miss something in the web service before? Do I have to map something into some variable?

  • In the code task I already receive an error at

var headers = ~{ch://OCAC_3aYYb1c2deL3D4O/headers};
"SyntaxError: Unexpected token ;" (received in the workflow log)
According to https://www.qualtrics.com/support/survey-platform/actions-module/code-task/#Setup this it should be possible when working with objects:
image.pngI'm confused.

Userlevel 4
Badge +18

https://community.qualtrics.com/XMcommunity/discussion/comment/41877#Comment_41877 I guess you have copy pasted the code exactly however the Task ID would be different for your case. Please try pipe in feature to get correct id, it should be different from what is given in above conversation(CAC_3aYYb1c2deL3D4O)
image.png

Badge +3

KimothiSaurabh
Some sessions with MaHuMhp were necessary to figure out different "stolpersteine". I promised to describe the solution here:

1) Authentication with a "Authenticated" web-service simply doesn't work
I used a basic auth account within a "Authenticated" web-service. I always received an "unauthorized" error. From our point of view this is a bug. Solution: simply use a "Not-Authenticated" web-service and add the decrypted authentication credentials as a header parameter (I used postman to find them out):
image.png(Workflow Step 1)

2) Don't forget the session ID /Cookie
Even though we think it's one workflow Qualtrics cannot link to the csrf token from step 1 within the final POST request. We must save csrf-token and session-ID first and use it in the POST request afterwards:
image.png(Workflow Step 2)
image.png(Workflow Step 3, "Non-Authenticated" as well btw)

3) Hand over the json-payload correctly to the interface
In addition I had some trouble in handing over my json-payload correctly (in my case I was trying to let Qualtrics create a new sales quote in C4C).
I finally did it by simply building the json as a string by using javascript directly in the form and pipe it into an "Embedded Data":

Qualtrics.SurveyEngine.setEmbeddedData("jsonpayload", json);

image.pngAt the end I used this content in Workflow step 3:
image.png
Hope this helps a bit to save some time ;-)
MaHuMhp, fyi.

Badge

Hi tried with the following steps but still its not working its showing as unexpected token error , how to resolve this?

Leave a Reply