#Cannot send code snippet to API. I get this bad request.

29 messages · Page 1 of 1 (latest)

young plume
#

Hello guys, i have a big problem. I am developing webapp for API in Blazor-server.
When i send normal message like:

"Show me example how to use Generic methods"

it works but if i go with snippet like this:

"
Does this code follow best practices:

    #region SearchBox
    public string SearchTerm { get; set; } = string.Empty;

    public List<PostEntity> FilteredPosts = new List<PostEntity>();
    public async Task FilterPosts()
    {
        await Task.Delay(0);

        if (!string.IsNullOrWhiteSpace(SearchTerm))
        {
            FilteredPosts = Posts
            .Where(x => x.Title.ToLower().Contains(SearchTerm.ToLower())).ToList();
        }
        else
        {
            FilteredPosts = new();
        }
    }
    #endregion

"

i get this error response:

"StatusCode: BadRequest, Content-Type: application/json, Content-Length: 476)"

I should format this text somehow? I cannot find information about this anywhere..

sick tree
#

badrequest indicates your api call is bad

young plume
#

I can see, but why if i put long question it gives me bad request, but if i go with short question it is not?

sick tree
#

how are you passing your prompt? like you may need to escape format special characters

young plume
sick tree
#

yeah your prompt message quotes will mess it up

young plume
#

Any suggestions?

sick tree
#

double em or use some json formatter if someone else will type text in your app

#

you can google json escape format quotes

young plume
#

I will, i thought about it, i tried with this:

       // var xmessage = System.Net.WebUtility.UrlEncode(message);

but this does not work

sick tree
#

thats for url's

young plume
#

This docs for API should be more specific.

sick tree
#

which docs? the error comes because your json is invalid 🙂

young plume
#

My json is valid - i can send short message and it works. If i put longer snippet it stop working

#

this does work

#

i can see in postman actually this snippet breaks my json

#

i need to fix it

sick tree
#

yes your " breaks it

young plume
#

actually there is no " " in this text

#

this is message:

Does this code follow best practices:

    #region SearchBox
    public string SearchTerm { get; set; } = string.Empty;

    public List<PostEntity> FilteredPosts = new List<PostEntity>();
    public async Task FilterPosts()
    {
        await Task.Delay(0);

        if (!string.IsNullOrWhiteSpace(SearchTerm))
        {
            FilteredPosts = Posts
            .Where(x => x.Title.ToLower().Contains(SearchTerm.ToLower())).ToList();
        }
        else
        {
            FilteredPosts = new();
        }
    }
    #endregion
#

and it break my json

#

something is wrong, but not " " as you can see screen in postman correct formatting should looks like this:

sick tree
#

you can use online validators to see whats wrong but you need to format it properly to be standard json

young plume
#

i will just use some library to serialize this

#

because now i am pasting string to json

#

instead i will make model class and serialize it

#

and this should do the job

sick tree
#

yep

young plume
#

thx for response