0

I have FastAPI request and with a text input of "how are you?" and it works on the swagger page without issue:

curl -X 'POST' \
  'https://xxxx.cloud/ai-chatbot/extract_text?q=how%20are%20you%3F' \
  -H 'accept: application/json' \
  -d ''

The issue is that when I pass "how are you?" on the ui, through the following JS code, I get 422 Unprocessable Entity. Here is the request:

var data = {
            "msg": msg+" d",
        }
        $.ajax({
            url: `${window.location.protocol}//${window.location.hostname}/ai-chatbot/extract_text?q=${data.msg+' d'}/1`,
            data: JSON.stringify(data),
            method: 'POST',
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            
            success: function (res) {
                var res = JSON.parse(res);
                drawMessage(res)
            },
            error: function (err) {
                if (err.desc == null)
                    err.desc = "Something went wrong!"
                toastr.error(err.desc)
            }
        })

what's the issue that the server side works well but the request on the UI gives 422 error?

the issue is due to having question mark at the end of request. I tried adding space but it didn't work.

Barmar
  • 741,623
  • 53
  • 500
  • 612
rara
  • 13
  • 2
  • 1
    Unless the value of `msg` in your JS code is supposed to be URL-encoded already, then that's likely to be the issue - that you are inserting this value into the URL, _without_ encoding it. – CBroe Jul 28 '23 at 14:05
  • @CBroe How do you think I should change the code to get a result? – rara Jul 28 '23 at 14:11
  • The body of the 422 error will tell you what is missing from your request. – MatsLindh Jul 28 '23 at 15:16
  • 2
    Use: ```q=${encodeURIComponent(data.msg+' d')}/1``` – Barmar Jul 28 '23 at 15:30
  • @Barmar it didn't work...the same error. – rara Jul 28 '23 at 18:15
  • In the `curl` command you don't send any data, the query is only in the URL. Why are you sending it with `$.ajax`? – Barmar Jul 28 '23 at 19:13
  • @Barmar =how%20are%20you%3F is the data. – rara Jul 28 '23 at 19:16
  • The `data:` option to `$.ajax` corresponds to the `-d` option in `curl`. That's blank. – Barmar Jul 28 '23 at 19:18
  • Why do you concatenate `d` to the message? You're appending it twice when you do `data.msg + ' d'`, since you already added the `d` when you created `data`. – Barmar Jul 28 '23 at 19:19

0 Answers0