console icon indicating copy to clipboard operation
console copied to clipboard

HTTP integration | assign HTTP header value

Open noraneto opened this issue 3 years ago • 21 comments

We have a need to assign key value from JSON message to a HTTP header or URL Params value.

noraneto avatar Aug 12 '21 08:08 noraneto

Can you provide an example of your json body and which key value needs to be pulled out?

vicmgs avatar Aug 12 '21 15:08 vicmgs

The example json attached. smpljson.txt We need following key values in URL Params: dev_eui, decoded.payload.token, reported_at.

noraneto avatar Aug 13 '21 07:08 noraneto

Screen Shot 2021-08-23 at 9 53 39 AM

@noraneto please try something like this in your integration and verify how it works out for you.

vicmgs avatar Aug 23 '21 16:08 vicmgs

Thanks, this works for URL Params.

noraneto avatar Aug 24 '21 10:08 noraneto

Assigning key value from JSON message to a HTTP header or URL Params value does not work anymore. Worked before, please check.

noraneto avatar Jul 03 '22 17:07 noraneto

what interpolation values are you trying to add? any more detail would be appreciated

vicmgs avatar Jul 05 '22 17:07 vicmgs

If you're using a custom template. You can only interpolate values present in the template into the URL Params and Header fields.

michaeldjeffrey avatar Jul 05 '22 18:07 michaeldjeffrey

I have stored a customer specific api-token as a device label when onboarding devices using console api. I also have another device label which defines the flow for the application. Using the custom script function I will read out the label for the api-token from the uplink_info and write it into the decoded payload key. So basically I have decoded.payload.token in my json and I need to assign it to the HTTP header. And yes I am using the custom template and I can read out the decoded.payload.token with it, but I do not want to send this token as a apart of the body. Would it be possible to read out json keys for the header similarly as in the custom template using double curly brackets?

noraneto avatar Jul 05 '22 19:07 noraneto

Sounds like you don't want the decoded.payload.token to appear in the integration json, but you want it to be interpolated in your http headers?

This is not possible right now as the interpolation only works for what results from the integration json.

vicmgs avatar Jul 05 '22 19:07 vicmgs

Yes I would like to interpolate keys from original json message into the http header. However as a workaround I tried to interpolate the keys from custom template that I am using, but it didn't work either.

noraneto avatar Jul 05 '22 20:07 noraneto

I also tested interpolating keys from json message to http header without using custom template and it didn't work. I am attaching the debug request. debug.txt

noraneto avatar Jul 05 '22 21:07 noraneto

Can you show us the interpolation header fields on your integration without using custom integration json? That would be more helpful

vicmgs avatar Jul 05 '22 21:07 vicmgs

image Here is how my header fields look.

noraneto avatar Jul 06 '22 04:07 noraneto

The interpolation seems to work only with URL params not with the headers.

noraneto avatar Jul 06 '22 04:07 noraneto

Header interpolation was added with https://github.com/helium/router/pull/816.

michaeldjeffrey avatar Jul 06 '22 21:07 michaeldjeffrey

Thank you for the fast response. Please consider making the interpolation work from the original JSON message (integration input) instead the integration result. It will make integration more flexible as the output (when templates are used) is always subset of original JSON message. Normally you do not want to send header or params keys duplicated in the body. Also when integrating with 3rd party APIs they usually do not allow send additional keys in the body.

noraneto avatar Jul 07 '22 06:07 noraneto

I'll have to think on that a bit. All existing integrations using this feature are pulling from the templated message. We have to make sure not to break those, without making it cumbersome to use.

michaeldjeffrey avatar Jul 07 '22 17:07 michaeldjeffrey

Thanks. There is an option to use triple curly brackets for interpolating, I do not recall what is the usage for that, but maybe something similar will do for pulling from the integration input.

noraneto avatar Jul 07 '22 18:07 noraneto

Triple curly brackets are part of the official mustache spec https://mustache.github.io/mustache.5.html for opting out of HTML escaping.

michaeldjeffrey avatar Jul 07 '22 18:07 michaeldjeffrey

Maybe it is possible interpolate from both jsons, integration input and output at the same time and if the same keys exist the output prevails?

noraneto avatar Jul 08 '22 07:07 noraneto

Hi In my case I am seeing that neither header nor URL interpolation is working. I am using a custom decoder. I have verified it works. The values are decoded correctly and interpolated correctly in the request body.

This is my flow: image

This is my decoder:

function Decoder(bytes, port, uplink_info) {
  var decoded={};
  try{
  var result = String.fromCharCode.apply(null, bytes);
  const parsedResult = JSON.parse(result);
  decoded.token = parsedResult.deviceToken;
  decoded.payload = parsedResult.sensorValue;
  return decoded;
  } catch (err) {
  return 'Decoder: ' + err.name + " : " + err.message;;
  }
 }

This is my custom template:

{
  "token": {{decoded.payload.token}},
  "payload": {{decoded.payload.payload}},
}

I have tried many possible variations in the hope that any of them worked. See image below for my header/params configuration Note that this is just something I did for helping with debugging. In my real scenario I only want a single JWT Authorization header with the value Bearer <token> . image

Now, when I send a message from my device, and look at the debug window in console. I see that the request body has been successfully interpolated. However the header and url parameters have not. The message content is {"deviceToken":"theToken","sensorValue":"theValue"}

This is what I see in the Debug tab (body as expected, headers and url params not interpolated):

{
  "id": "a8b67e42-c478-4ff9-8e3c-d3072747aaf8",
  "name": "RequestBin",
  "status": "success"
}
{
  "body": "{\n  \"token\": theToken,\n  \"payload\": theValue,\n}",
  "headers": {
    "Authorization": "Bearer {{token}}",
    "Authorization1": "Bearer {{decoded.payload.token}}",
    "Authorization2": "{{decoded.payload.token}}",
    "Authorization3": "{{token}}",
    "Content-Type": "application/json"
  },
  "method": "post",
  "url": "https://eo41m4s5k7ej6yi.m.pipedream.net",
  "url_params": {
    "token": "Bearer {{token}}",
    "token1": "Bearer {{decoded.payload.token}}",
    "token2": "{{decoded.payload.token}}",
    "token3": "{{token}}"
  }
}

image

as-iotex avatar Sep 15 '23 14:09 as-iotex