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: (#225) Add support for application/json in POST requests #234

Merged
merged 1 commit into from
Aug 9, 2024

Conversation

khoaHyh
Copy link
Contributor

@khoaHyh khoaHyh commented Aug 9, 2024

Addresses #225

🗒️ Summary

  • Modified _from_body function to handle JSON data
    • Implement conditional processing for JSON and form data
  • Added test case for JSON POST request

Demo

I used the Python virtual env and installed my custom branch as a package to sanity check the changes E2E. I was able to get the same error using the curl call and the same code mentioned in the GH Issue. After the changes, the curl call worked 🤠

🔥 Before (expand to see screenshot)

Screenshot from 2024-08-09 11-52-53

🪄 After (expand to see screenshot)

Screenshot from 2024-08-09 11-49-22

TDD

Initially, the test I added failed because the JSON parsing wasn't implemented yet. The screenshot below is a snippet of the error from the test before the changes were added.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[78], line 21
     18     assert "Bar: 15" in response
     20 # Run the test
---> 21 test_json_post()

Cell In[78], line 13, in test_json_post()
     12 def test_json_post():
---> 13     response = test_r(cli, '/', '{"foo": "Lorem", "bar": 15}', 'post',
     14                       headers={"Content-Type": "application/json"})
     16     assert "It worked!" in response
     17     assert "Foo: Lorem" in response

Cell In[70], line 3, in test_r(cli, path, exp, meth, hx, **kwargs)
      1 def test_r(cli, path, exp, meth='get', hx=False, **kwargs):
      2     if hx: kwargs['headers'] = {'hx-request':"1"}
----> 3     test_eq(getattr(cli, meth)(path, **kwargs).text, exp)

### inbetween stack trace stuff here ###

Cell In[34], line 8, in _from_body(req, p)
      6 d = _annotations(anno)
      7 cargs = {k:_form_arg(k, v, d) for k,v in form2dict(form).items() if not d or k in d}
----> 8 return anno(**cargs)

TypeError: Item.__init__() missing 2 required positional arguments: 'foo' and 'bar'

- modify _from_body function to handle JSON data
- add test case for JSON POST request
Copy link

gitnotebooks bot commented Aug 9, 2024

Found 1 changed notebook. Review the changes at https://gitnotebooks.com/AnswerDotAI/fasthtml/pull/234

@jph00
Copy link
Contributor

jph00 commented Aug 9, 2024

Great stuff!

@jph00 jph00 added the enhancement New feature or request label Aug 9, 2024
@jph00 jph00 merged commit 55f588d into AnswerDotAI:main Aug 9, 2024
1 check passed
@khoaHyh khoaHyh deleted the feat/225 branch August 9, 2024 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants