question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BAD_HEADER error when receive message with headers from java client

See original GitHub issue

Hi! I faced with problem of this error while send message from java client to js client with headers.

Nats Server:

nats_1              | [1] 2021/06/01 14:24:20.505051 [INF] Starting nats-server
nats_1              | [1] 2021/06/01 14:24:20.505264 [INF]   Version:  2.2.6
nats_1              | [1] 2021/06/01 14:24:20.505285 [INF]   Git:      [cf433ae]
nats_1              | [1] 2021/06/01 14:24:20.505489 [DBG]   Go build: go1.16.4

Logs from nats server:

nats_1              | [1] 2021/06/01 14:04:51.340544 [TRC] 192.168.0.1:40860 - cid:3 - "v2.11.3:java:nats://localhost:4222" - <<- [HPUB realtime_control 23 237]
nats_1              | [1] 2021/06/01 14:04:51.340809 [TRC] 192.168.0.1:40860 - cid:3 - "v2.11.3:java:nats://localhost:4222" - <<- MSG_PAYLOAD: ["NATS/1.0\r\nkey:value\r\n\r\n{\"controlStreamName\":\"audio_xAYYyszRUP\",\"accountId\":\"********-****-****-****-************\",\"timestamp\":\"2021-06-01T14:04:51Z\",\"asrConfig\":{\"language\":\"english\"},\"checklistId\":\"********-****-****-****-************\"}"]
nats_1              | [1] 2021/06/01 14:04:51.341485 [TRC] 192.168.0.11:38402 - cid:4 - "v2.0.4:nats.js" - ->> [HMSG realtime_control 1 23 237]
nats_1              | [1] 2021/06/01 14:04:51.341748 [TRC] 192.168.0.12:43554 - cid:5 - "v2.0.4:nats.js" - ->> [HMSG realtime_control 1 23 237]

Logs from js server

node-server_1      | > HMSG realtime_control 1 23 237␍␊NATS/1.0␍␊key:value␍␊␍␊{"controlStreamName":"audio_xAYYyszRUP","accountId":"********-****-****-****-************","timestamp":"2021-06-01T14:04:51Z","asrConfig":{"language":"english"},"checklistId":"********-****-****-****-************"}␍␊
node-server_1      | {"message":"Unhandled Rejection at: Promise {\n  <rejected> Error: Error receiving message from realtime_control: {\"name\":\"NatsError\",\"code\":\"BAD_HEADER\"}\n      at /opt/node-server/lib-streamer/lib/NatsClient.js:85:17\n      at processTicksAndRejections (internal/process/task_queues.js:93:5)\n} reason: Error: Error receiving message from realtime_control: {\"name\":\"NatsError\",\"code\":\"BAD_HEADER\"}\n    at /opt/node-server/lib-streamer/lib/NatsClient.js:85:17\n    at processTicksAndRejections (internal/process/task_queues.js:93:5)","level":"error"}

Example of java code sending:

    Message msg = NatsMessage.builder()
            .subject(topic)
            .headers(new Headers().add("key", "value"))
            .data(message.getPayload())
            .build();
    connection.publish(msg);

Example of js code consuming

async subscribe({
    topic,
    queue,
    callback
  }) {
    try {
      const subscription = this.nc.subscribe(topic, {
        queue
      });
      this.subscriptions.set(topic, subscription);

      (async () => {
        try {
          for await (const message of subscription) {
            const headers = {};

            if (message.headers) {
              for (const [key, value] of message.headers) { 
                if (Array.isArray(value) && value.length === 1) {
                  headers[key] = value[0];
                } else {
                  headers[key] = value;
                }
              }
            }

            callback(null, {
              topic,
              headers,
              body: Buffer.from(message.data)
            });
          }
        } catch (err) {
          throw Error(`Error receiving message from ${topic}:` + ` ${JSON.stringify(err)}`);
        }
      })();
    } catch (err) {
      throw Error(`Error subscribing to ${topic}: ${JSON.stringify(err)}`);
    }
  }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
aricartcommented, Jun 2, 2021

@StanislavGorobzeev npm install nats@latest to get the fix

0reactions
aricartcommented, Jun 1, 2021

OK that change would be simpler.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exception for bad header can leak an Authorization secret
If there's an illegal character in a header value, an IllegalArgumentException is thrown whose message includes the full header value.
Read more >
Bad header in CXF - java - Stack Overflow
After a lots of test, i found that the "Fluent interface" is not really working like it should, it seem that the order...
Read more >
StompHeaders (Spring Framework 6.0.3 API)
public class StompHeaders extends Object implements MultiValueMap<String ... Get the message-id header. ... Methods inherited from interface java.util.
Read more >
zen unicast handshake failed with an exception caused by ...
Does anyone know what can cause a “bad header found”? ... (I don't see the “invalid header found” message, just the “Error validating ......
Read more >
Headers with the NATS.io Java Library
With the release of the 2.10.0 Java client , Headers are completely ... You can check if the message has Headers, and get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found