Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http/request): allow access to the original request #649

Merged
merged 3 commits into from
May 30, 2022

Conversation

crookse
Copy link
Member

@crookse crookse commented May 29, 2022

Closes #604

TODO

Summary

  • Makes Drash.Request.original property to store the original Request
  • Drash.Request.original can be used to access the raw request body
  • Drash.Request.parseBody() does not make Drash.Request.original.bodyUsed to be true, so it can be parsed manually by the user

Other Notes

This will allow users to access the original if needed. For example, #604 requests access to the raw body. This can now be done like so:

import * as Drash from "...";

class SomeResource extends Drash.Resource {
  public paths = ["/something"];

  public async POST(req: Drash.Request, res: Drash.Response): Promise<void> {
    // Access Drash's internal request helper method to get the body params
    console.log(req.bodyAll()); // Reads the Drash.Request body
    console.log(req.bodyUsed);  // true

    // Now access the original and parse the body manually
    console.log(req.original.bodyUsed);     // false --> The original request body is intact and has not been read yet
    // Now read the original request body
    console.log(await req.original.json()); // { hello: "world" }
    console.log(req.original.bodyUsed);     // true ---> Now the original request body has been read

    res.text("GET /something");
  }
}

const server = new Drash.Server({
  hostname: "localhost",
  port: 1447,
  protocol: "http",
  resources: [SomeResource],
});

server.run();

console.log(`Server running at ${server.address}.`);

Example curl request for the above sample application:

$ curl -X POST -H 'Content-Type: application/json' localhost:1447/something --data '{"hello":"world"}'

@crookse crookse added the Type: Minor Merging this pull request results in a minor version increment label May 29, 2022
Copy link
Member

@Guergeiro Guergeiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Go Go!

@crookse crookse force-pushed the feat/allow-request-body-access branch from caa5cb3 to f4a8a99 Compare May 30, 2022 10:32
@crookse crookse merged commit 30d98f0 into main May 30, 2022
@Guergeiro Guergeiro deleted the feat/allow-request-body-access branch May 30, 2022 11:29
@bradenmacdonald
Copy link

Nice, thanks!

@crookse crookse mentioned this pull request Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Minor Merging this pull request results in a minor version increment
Development

Successfully merging this pull request may close these issues.

feat: access raw request.body
3 participants