Gura
Gura

Reputation: 15

Sharepoint RestAPI Error: A 'PrimitiveValue' node or 'StartObject' node was expected

I am currently trying to update a Sharepoint Lists item image column using Sharepoint RestAPI. The image is already saved to a list item as a file attachment, so I just need the thumbnail to show on the image column.

I keep getting the same errors like "When attempting to read the value of a property, a 'StartArray' node was read from the JSON reader, but a 'PrimitiveValue' node or 'StartObject' node was expected."

Please help me what is wrong with my code. Thank you in advance!

let body: [String: Any] = [
    "__metadata": [
        "type":"SP.Data.TestListListItem"
    ],
    "image3": [
        "{\"serverRelativeUrl\":\"/sites/{siteName}/Lists/TestList/Attachments/20/Reserved_ImageAttachment_testImage\",\"type\":\"thumbnail\"}"
    ]
]

var dataBody = Data()
do {
    dataBody = try JSONSerialization.data(withJSONObject: body, options: [])
} catch {
    print("error")
    return
}

let urlString = "{siteName}/_api/web/lists/GetByTitle('TestList')/items(20)"

guard let url = URL(string: urlString) else {
    print("Invalid URL")
    exit(1)
}
var request = URLRequest(url: url)

request.httpMethod = "POST"
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
request.setValue("application/json;odata=verbose", forHTTPHeaderField: "Accept")
request.setValue("application/json;odata=verbose", forHTTPHeaderField: "Content-Type")
request.setValue("*", forHTTPHeaderField: "If-Match")
request.setValue("MERGE", forHTTPHeaderField: "X-HTTP-Method")
request.setValue(formDigestValue, forHTTPHeaderField: "X-RequestDigest")
request.httpBody = dataBody

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
    if let error = error {
        print("Error: \(error)")
        return
    }
    
    if let data = data {
        do {
            let json = try JSON(data: data)
        } catch {
            print("Error parsing JSON: \(error)")
        }
    }
}
task.resume()

Upvotes: 1

Views: 152

Answers (0)

Related Questions