# HN6 (Fetch with an unknown topic)

**URL:** <https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177>\
**Category:** General\
**Tags:** challenge:kafka\
**Created:** [October 5, 2024, 3:53am UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177 "2024-10-05T03:53:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntquang98](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ntquang98/32/2137_2.png) [@ntquang98](https://forum.codecrafters.io/u/ntquang98)\
**Post date:** [October 5, 2024, 3:53am UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177/1 "2024-10-05T03:53:09Z")

</div>

Hello everyone and thank you for helping check my issue  
Currently, I am in HN6 of challenge Kafka, I receive the error about reduced byte after decoding the FetchResponse. It is from bytes 12 to 27 which is the location of the topic ID  
The error:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/c/c0da7c5f4d5a7d5f3fd566a8aceeb3f34883ad54.png)  
The body creates

```auto
func (r *FetchResponse) ToBytes() ([]byte, error) {
	buf := new(bytes.Buffer)
	// tagged_fields
	if err := binary.Write(buf, binary.BigEndian, int8(0)); err != nil {
		return nil, err
	}

	// write throttle_time_ms
	if err := binary.Write(buf, binary.BigEndian, r.ThrottleTimeMs); err != nil {
		return nil, err
	}

	// write error_code
	if err := binary.Write(buf, binary.BigEndian, r.ErrorCode); err != nil {
		return nil, err
	}

	// write session_id
	if err := binary.Write(buf, binary.BigEndian, r.SessionID); err != nil {
		return nil, err
	}

	// write num_of_responses
	if err := binary.Write(buf, binary.BigEndian, int8(len(r.Topics)+1)); err != nil {
		return nil, err
	}

	for _, topic := range r.Topics {
		// write topic_id

		if err := binary.Write(buf, binary.BigEndian, topic.TopicID); err != nil {
			return nil, err
		}

		// write num_of_partitions
		if err := binary.Write(buf, binary.BigEndian, int8(len(topic.Partitions)+1)); err != nil {
			return nil, err
		}

		for _, part := range topic.Partitions {
			// write partition_index
			if err := binary.Write(buf, binary.BigEndian, part.PartitionIndex); err != nil {
				return nil, err
			}

			// write error_code
			if err := binary.Write(buf, binary.BigEndian, part.ErrorCode); err != nil {
				return nil, err
			}

			// // write high_watermark
			// if err := binary.Write(buf, binary.BigEndian, int64(0)); err != nil {
			// return nil, err
			// }

			// // write last_stable_offset
			// if err := binary.Write(buf, binary.BigEndian, int64(0)); err != nil {
			// return nil, err
			// }

			// // write log_start_offset
			// if err := binary.Write(buf, binary.BigEndian, int64(0)); err != nil {
			// return nil, err
			// }

			// write aborted_transactions - length 0
			// if err := binary.Write(buf, binary.BigEndian, int8(1)); err != nil {
			// return nil, err
			// }

			// write producer_id
			// if err := binary.Write(buf, binary.BigEndian, int64(0)); err != nil {
			// return nil, err
			// }

			// write first_offset
			// if err := binary.Write(buf, binary.BigEndian, int64(0)); err != nil {
			// return nil, err
			// }

			// // _tagged_fields
			// if err := binary.Write(buf, binary.BigEndian, int8(0)); err != nil {
			// return nil, err
			// }

			// // write preferred_read_replica
			// if err := binary.Write(buf, binary.BigEndian, int32(0)); err != nil {
			// return nil, err
			// }

			// // write records_size
			// if err := binary.Write(buf, binary.BigEndian, int8(0)); err != nil {
			// return nil, err
			// }

			// _tagged_fields
			if err := binary.Write(buf, binary.BigEndian, int8(0)); err != nil {
				return nil, err
			}
		}

		// tag_buffer 1 byte - end of topic
		if err := binary.Write(buf, binary.BigEndian, int8(0)); err != nil {
			return nil, err
		}
	}

	// tag_buffer
	if err := binary.Write(buf, binary.BigEndian, int8(0)); err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}

```

about the topic id uuid, I get it from the request body from buf[23: 23+16]

```auto
// parse body
	maxWaitMs := req.body[0:4]
	minBytes := req.body[4:8]
	maxBytes := req.body[8:12]
	isolationLevel := req.body[12:13]
	sessionID := req.body[13:17]
	sessionEpoch := req.body[17:21]
	topicLength := req.body[21:23]
	topicID := req.body[23 : 23+16]

```

How can I fix the error, please help.  
thanks again and sorry if my English wasn’t good

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 5, 2024, 6:19am UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177/2 "2024-10-05T06:19:02Z")

</div>

@ntquang98 Could you put your code on GitHub and share a link? It’s easier to debug if I can run your code. 🤝

---

<div class="post-metadata">

**Author:** ![ntquang98](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ntquang98/32/2137_2.png) [@ntquang98](https://forum.codecrafters.io/u/ntquang98)\
**Post date:** [October 5, 2024, 7:28am UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177/3 "2024-10-05T07:28:23Z")

</div>

Hi @andy1li, thanks for replyed. Here is my code [codecrafters-kafka/app/server.go at main · ntquang98/codecrafters-kafka · GitHub](https://github.com/ntquang98/codecrafters-kafka/blob/main/app/server.go)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 5, 2024, 10:11am UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177/4 "2024-10-05T10:11:14Z")

</div>

@ntquang98 Got it. I’ll take a look on Monday.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 8, 2024, 2:08am UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177/6 "2024-10-08T02:08:32Z")

</div>

@ntquang98 I tried running your code, but unfortunately, it couldn’t pass the 2nd stage anymore. Fixing this first might help with the issue in the later stages:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/3/39198e032f2e678d91fe84f1affc983a094e80d0.jpeg)

Suggestion: use our [CLI](https://docs.codecrafters.io/cli/installation) to run tests against previous stages like this:

```auto
codecrafters test --previous

```

---

<div class="post-metadata">

**Author:** ![ntquang98](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ntquang98/32/2137_2.png) [@ntquang98](https://forum.codecrafters.io/u/ntquang98)\
**Post date:** [October 8, 2024, 2:51pm UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177/8 "2024-10-08T14:51:15Z")

</div>

Thank you, I found the problem

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [October 13, 2024, 2:51pm UTC](https://forum.codecrafters.io/t/hn6-fetch-with-an-unknown-topic/2177/9 "2024-10-13T14:51:20Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
