MarkyRoden
MarkyRoden

Reputation: 1074

How can I post a complex character to SharePoint REST API?

Whenever I try a post a complex character to create a list entry in an O365 SharePoint list (via REST API) I get a JSON parse error from the server. The following is the simple post and it is the β (beta) character which causes the fail. &mdash (—) and other non-simple characters also cause the fail.

The code works just fine for alphabetic characters. It appears to me to be a parsing issue on the SharePoint side but I wanted to know if I was missing something stupid (it happens...)

If I remove the β character from Title field it works just fine. If I create the list item manually through the SP web interface it works just fine, so I know that it is not that the character is invalid, just the creation of the list item through the API.

The headers for the post are:

var outHeaders = {
    "Content-Type": "application/json;odata=verbose",
    "Accept": "application/json;odata=verbose",
    "Authorization": 'Bearer ' + token,
    "Content-Length": data.length,
    "X-RequestDigest": digest,
    "IF-MATCH" : "*"
}

The data being posted is as follows

{
    "__metadata": {
        "type": "SP.Data.EmailArchiveListItem"
    },
    "Title": "TEST fail email β",
    "Sender": "Mark Roden",
    "Recipient": "Mark Roden",
    "Body": "HI Marky"
}

The error returned is:

400 Bad Request

{
    "error": {
        "code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
        "message": {
            "lang": "en-US",
            "value": "Invalid JSON. Unexpected end of input was found in JSON content. Not all object and array scopes were closed."
        }
    }
}

Any input/thoughts appreciated.

Upvotes: 0

Views: 1470

Answers (4)

Code4Rice
Code4Rice

Reputation: 51

for folder name and file name, use escaped string instead.

'#' => %23

'β' => %CE%B2

Upvotes: 0

Adrian
Adrian

Reputation: 11

In 2021 (five years later) I had a similar problem (using the SharePoint MS Graph REST API) which I fixed by specifying the charset in the content-type explicitly:

application/json; charset=utf-8

Upvotes: 1

MarkyRoden
MarkyRoden

Reputation: 1074

Joe Jorden's answer at least got me this far - If you post the β or — into an RTF Field it works - posting it into a plain text field does not.

enter image description here

Upvotes: 0

CodeQuake
CodeQuake

Reputation: 106

Try using this code: β That should make it through.

Upvotes: 2

Related Questions