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

Question: Body-Parser #672

Closed
frocentus opened this issue Jan 23, 2021 · 5 comments · Fixed by #673
Closed

Question: Body-Parser #672

frocentus opened this issue Jan 23, 2021 · 5 comments · Fixed by #673
Assignees
Labels

Comments

@frocentus
Copy link

Hi,

I do not unterstand fully the concept of the Body-Parser-Middleware and their activation on a specific Route.

Are there any Built-In Body-Parsers (for JSON or Form-Data) in Pode 2.0.3? I looked through the source, but got lost :)

I unterstand, that I can implement Body-Parsers per Content-Type by myself - but I wanted to ask before I dig into that.

I tried it to test it with a simple Route, which gets its data (phone=680+12345678) from a Post from an HTML-From with Content-Type 'application/x-www-form-urlencoded':

Add-PodeRoute -Method Post -Path "2fa" -ScriptBlock { Write-PodeJsonResponse -Value @{ 'Data' = $WebEvent.Data } }

thanks in advance

@frocentus
Copy link
Author

Ok, after reading Docs again and searching through the Source-Code if found that there are inbuilt parsers - so I was just wondering now, why my body does not get parsed.

I returned in my route the Request-Object as a Json-Response and found, that there is the Content-Type field empty.

I am still investigating, why in my setup (involving a custom reverse proxy) Pode does not get the Content-Type (the Browser sends it)

@frocentus
Copy link
Author

After reading Docs again, and searching to the code again, I feel quite stupid - I was missing the ContentType Parameter in my route definition.

So I am closing this :)

@Badgerati
Copy link
Owner

Hey,

Yes you're right, Pode does have inbuilt body parsing middleware for the more common types: JSON, XML, and Forms. The parsing is chosen based on the Content-Type supplied on the Request. You can supply the -ContentType on Add-PodeRoute to force a specific content-type if needed.

If the $WebEvent.Request object's ContentType property is empty, this could suggest that the proxy is removing the header before it gets to Pode 🤔

@frocentus
Copy link
Author

Thank you for your answer, with that additional Content-Type Parameter it works like a charm.

I dug a little bit deeper:
The Reverse-Proxy sets the Header, but in lowercase 'content-type'. Could the lowercase headername be the problem?

As for my unterstanding is, that the .net Hashtable (used in PodeHttpRequest.cs) is case sensitve, but HTTP-headernames should be case insensitive.

A possible solution could be to set the compare-function on the hashtable like mentioned on this SO-Post: https://stackoverflow.com/questions/856989/using-hashtables-dictionaries-with-string-keys-case-insensitive-searching

@Badgerati
Copy link
Owner

Oh wow, good spot! Too used to PowerShell's Hashtables 🙈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants