Rest API Post works in Postman,and throws "Invalid Characters" from Browser

I’m creating a web form so users can send requests to Jira and only using the Summary And Description fields so far. Summary works fine and updates, but Description has been throwing Invalid Char errors every time, unless I sub the dynamic payload constructor for a static version, which works fine, and it works fine in postman.

If i hard code text in place of where the “description” variable goes it works fine, and when print the var text to the console it’s not adding anything funky, so quite frustrating.

const payload = {
            fields: {
                summary: summary,
                description: {
                    type: "doc",
                    version: 1,
                    content: [
                        {
                            type: "paragraph",
                            content: [
                                {
                                    type: "text",
                                    text: description
                                }
                            ]
                        }
                    ]
                },
                issuetype: { name: "My Issue Type" },
                project: { key: "MYPROJ" }
            }
        };

Hello @gnothom

You haven’t advised which REST API endpoint you are sending the request to, but based on your sample request containing an ADF formatted description field body, can it be assumed it’s a Jira v3 REST API endpoint?

You haven’t provided any information about what this ‘dynamic payload constructor’ does, but can it be assumed it somehow takes the text typed by the user into the form field and converts it into ADF formatted text in JSON?

You haven’t provided any sample of the text created by that ‘constructor’ for anyone to determine one way or another whether the ADF content for the description field is or is not ‘funky’.

What, specifically, throws this error and where does it get displayed? The v3 endpoints return numerical, 400 series errors and I’m not aware of any v3 endpoint returning any error text that contains the phrase “Invalid Character”

With all this information missing from your question, all you’ve done is make a problem statement. Refer to the How to ask a good question thread for guidance on an acceptable level of information.

PS. In your sample request, I can see at least one mistake. For the paragraph’s content object, the text sub-object is not wrapped in inverted commas, so if the text from the user contained a comma, that would immediately break the JSON structure.

Hi @sunnyape thank you for taking a look. Will fill in some of the missing info here, and hopefully it will help shed some light on the problem

Endpoint: rest/api/3/issue

Origin of the description text:
<textarea id="description" name="description" class="wide-input"></textarea>

Conversion to var:
const descriptionText = document.getElementById('description').value.trim();

Console print for a failing request: “This is a test description.”

Dev Tools Error thrown: 400 Bad Request
Jira output: “Error response from Jira: { errorMessages: [ ‘INVALID_INPUT’ ], errors: {} }”

Is this enough to help diagnose further?
Thank You!

Thanks for providing some extra information about some of the constructs of the form and how the textarea field’s value is converted to a variable, but it’s still unknown how the actual request is being created / assembled before being sent to the API endpoint.

HOW does the request get sent? WHAT is sending it? Some JavaScript? A browser add-in? Something else?

To be blunt, a 400 error with an INVALID_INPUT is simply the API telling you that the body of the request contains mal-formed JSON (such as the missing inverted commas around the text string object I’ve already mentioned) and it can’t process it. Here is a similar thread on the same topic.

What sanitisation / stringification is done to the descriptionText variable to encode any special characters? What is done to convert the string variable into ADF formatted JSON for use in the request body?

Testing your JSON request body with a tool, like this one , will immediately show you where the problem lies. Tools like this one will even fix the faulty JSON and tell you where the fault was.

Once you have confirmed that the JSON request body is correct, you can use the Document Viewer tool to test if the ADF is structured correctly.

Well it appears my problem with with the http headers, not actually sure what the problem is, but I removed what I had and added the bare minimum 'Content-Type': 'application/json' and voila, it started working.

Thanks @sunnyape for your help.

1 Like