The Wayback Machine - https://web.archive.org/web/20201206190640/https://github.com/falconry/falcon/issues/1636
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

User guide/FAQ: Send JSON & URL-encoded requests to the App #1636

Open
yarin177 opened this issue Jan 11, 2020 · 6 comments
Open

User guide/FAQ: Send JSON & URL-encoded requests to the App #1636

yarin177 opened this issue Jan 11, 2020 · 6 comments

Comments

@yarin177
Copy link

@yarin177 yarin177 commented Jan 11, 2020

Hey, I'm trying to fetch out the data with JSON I've spent way too much time on google and tried every single solution with no success.. when I send the request it raises an json.decoder.JSONDecodeError Exception.
maybe can you help?

class TextOcrRes:
    def on_post(self, req, resp):
        """Handles Text POST requests"""
        json_data = json.loads(req.bounded_stream.read().decode("utf-8")) # json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
        print(json_data)

What I'm sending:

r = requests.post("http://x.xx.xx.xx/translate", data={'image_data':'message'})
I'm on python 3.6.8

@open-collective-bot
Copy link

@open-collective-bot open-collective-bot bot commented Jan 11, 2020

Hi 👋,

Thanks for using Falcon. The large amount of time and effort needed to
maintain the project and develop new features is not sustainable without
the generous financial support of community members like you.

Please consider helping us secure the future of the Falcon framework with a
one-time or recurring donation.

Thank you for your support!

@yarin177 yarin177 changed the title Falcon get stuck on json.loads while reading req.stream.read() Falcon json.decoder.JSONDecodeError Jan 11, 2020
@vytas7
Copy link
Member

@vytas7 vytas7 commented Jan 11, 2020

Hi @yarin177 !
Your application code looks just fine!
You could simplify it somewhat by using the Falcon media handlers, but the choice is yours 🙂

I would guess the problem is that you are sending your request payload as URL-encoded (application/x-www-form-urlencoded) instead of JSON.

The following snippet works for me (I just extended your example slightly to showcase media, but your original responder is also left intact):

import json

import falcon


class TextOcrRes:
    def on_post(self, req, resp):
        """Handles Text POST requests"""
        json_data = json.loads(req.bounded_stream.read().decode("utf-8"))
        print(json_data)


class TextOcrMediaRes:
    def on_post(self, req, resp):
        """Showcase Falcon media framework"""
        print(req.media)


app = falcon.API()
app.add_route('/translate', TextOcrRes())
app.add_route('/translate2', TextOcrMediaRes())
>>> requests.post('http://localhost:8000/translate', json={'image_data':'message1'})
<Response [200]>
>>> requests.post('http://localhost:8000/translate2', json={'image_data':'message2'})
<Response [200]>

I'll leave the issue open for a while.
Maybe we should add an example in our tutorial how to actually send a JSON request?
IIRC you're not first to get confused by this, even though it is not strictly a Falcon issue.

@vytas7 vytas7 changed the title Falcon json.decoder.JSONDecodeError User guide/FAQ: show how to send a JSON request to the App Jan 11, 2020
@vytas7 vytas7 changed the title User guide/FAQ: show how to send a JSON request to the App User guide/FAQ: Send JSON & URL-encoded requests to the App Jan 11, 2020
@yarin177
Copy link
Author

@yarin177 yarin177 commented Jan 11, 2020

Thank you, that solved the issue for me, and yes I'm sure it will be helpful if you can include an example for sending JSON in the tutorial.
I'm just curious, when will you send "data" for the server?

@vytas7
Copy link
Member

@vytas7 vytas7 commented Jan 11, 2020

@yarin177 Whenever an applications expect it, or at least supports it, and you want to test it 🙂

Although newly designed RESTful APIs typically prefer JSON (or YAML, Msgpack etc), application/x-www-form-urlencoded is still ubiquitous since it is the default content type for an HTML <form> that the web browser POSTs upon clicking the submit button.

@yarin177
Copy link
Author

@yarin177 yarin177 commented Jan 11, 2020

@yarin177 Whenever an applications expect it, or at least supports it, and you want to test it 🙂

Although newly designed RESTful APIs typically prefer JSON (or YAML, Msgpack etc), application/x-www-form-urlencoded is still ubiquitous since it is the default content type for an HTML <form> that the web browser POSTs upon clicking the submit button.

about your other solution using media, I'm basically trying to send over an image from a client to the server and save it. but I don't know if I should send it with JSON or DATA or FILES
I tried this on the client:

data = open('test.png','rb').read()
r = requests.post("http://localhost:8000/translate", data=data)

But How do I receive it on the server? do I need to encode it, JSON?

@vytas7
Copy link
Member

@vytas7 vytas7 commented Jan 11, 2020

Answered the sending image question on #1637.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.