# Error: Expected: HTTP-version, Received: ""

**URL:** <https://forum.codecrafters.io/t/error-expected-http-version-received/1480>\
**Category:** Challenges\
**Tags:** challenge:http-server\
**Created:** [August 24, 2024, 9:46am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received/1480 "2024-08-24T09:46:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anusinghg](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/anusinghg/32/1365_2.png) [@anusinghg](https://forum.codecrafters.io/u/anusinghg)\
**Post date:** [August 24, 2024, 9:46am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received/1480/1 "2024-08-24T09:46:51Z")

</div>

I’m stuck on Stage #qv8.

I’ve tried checking the file content. The last couple of lines check the contents of the file being written and I’m getting the correct content back. Not sure what the error is about though.

Here are my logs:

```auto
remote: [your_program] /tmp/data/codecrafters.io/http-server-tester/pear_orange_raspberry_pear
remote: [tester::#QV8] Failed to read response: 
remote: [tester::#QV8] Received: "" (no content received)
remote: [tester::#QV8] ^ error
remote: [tester::#QV8] Error: Expected: HTTP-version, Received: ""
remote: [tester::#QV8] Test failed
remote: [tester::#QV8] Terminating program
remote: [your_program] checking
remote: [your_program] grape strawberry banana apple raspberry pineapple strawberry banana
remote: [tester::#QV8] Program terminated successfully
remote: 

```

And here’s a snippet of my code:

```java
                    try {
                        in.readLine();
                        in.readLine();
                        String[] path = lines[1].split("/");
                        String fileName = path[2];
                        String content = in.readLine();
                        String filePath = commandLine.getArgList().get(0) + fileName;
                        System.out.println(filePath);
                        FileReaderUtil fileReaderUtil = new FileReaderUtil(filePath);
                        fileReaderUtil.writeStringToFileAndCreateDirectory(content);
                        System.out.println("checking");
                        FileReaderUtil fileReaderUtilCheck = new FileReaderUtil(filePath);
                        String fileContent = fileReaderUtilCheck.readFileAsString();
                        System.out.println(fileContent);
                        this.clientSocket.getOutputStream().write(response.get("fileCreated").getBytes());
                    }catch (IOException e){
                        this.clientSocket.getOutputStream().write(response.get("notFound").getBytes());
                        e.printStackTrace();
                    }

```

---

<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:** [August 26, 2024, 2:12am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received/1480/7 "2024-08-26T02:12:43Z")

</div>

Great job! The logic flow of your code is clear and correct.

However, the line `String content = in.readLine()` may not be entirely safe. It assumes the file content (1) contains only one line and (2) ends with a newline, which isn’t always the case.

Consider reading exact `n` bytes as specified in the request header instead:

`Content-Length: n`

* * *

#### Debug re-enactment:

- **Problem** : The code was responding correctly, but the tester did not receive anything, as indicated by `[tester::#QV8] Received: "" (no content received)`. Why?!

- **Line of Attack:** What if we ignore the file processing temporarily, and focus only on ensuring that the tester receive our response first?

- **Brute-Force:** I repeatedly inserted the `getOutputStream().write` line at every position of the code.

- **Intermediate result:**

- **Target Acquired** : So `String content = in.readLine()` must have been the culprit, causing the tester to start reading our response before we were able to send it.

- **Nitty-gritty** : What exactly happened when `String content = in.readLine()` was run?

---

<div class="post-metadata">

**Author:** ![anusinghg](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/anusinghg/32/1365_2.png) [@anusinghg](https://forum.codecrafters.io/u/anusinghg)\
**Post date:** [August 26, 2024, 4:38am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received/1480/8 "2024-08-26T04:38:45Z")

</div>

@andy1li This was the issue, it’s resolved now. Thank You for a detailed explanation of the issue and a potential solution. 🙂

---

<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:** [August 31, 2024, 5:01am UTC](https://forum.codecrafters.io/t/error-expected-http-version-received/1480/10 "2024-08-31T05:01:27Z")

</div>

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