Forum - “Update Bib Record” API not working with JSON?
- This topic has 6 replies, 2 voices, and was last updated 1 month, 4 weeks ago by Ori Miller.
- AuthorPosts
- August 18, 2015 at 7:34 pm #34074AnonymousInactive
Has anyone been able to update bib (or holdings) records using the JSON API? I have a test case that keeps failing with my JSON client, so I decided to try a simple test in the console with curl: First, retrieve a bib record with a get request in XML format, and then send the record back (unmodified) with a put request. Second, retrieve the same bib record with a get request in JSON format, and then send the record back (unmodified) with a put request. Below are the results of my tests.
This works (fetching and updating an XML bib object):
Code:curl -H "Authorization: apikey $ALMA_API_KEY" -H ‘Accept: application/xml’ -H ‘Content-Type: application/xml’ -X GET ‘https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/9930179940001701’ > 9930179940001701.xmlcurl -H "Authorization: apikey $ALMA_API_KEY" -H ‘Accept: application/xml’ -H ‘Content-Type: application/xml’ -X PUT ‘https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/9930179940001701’ -d @9930179940001701.xml
<successful xml response truncated for brevity>But this does *not* work (fetching and updating a JSON bib object):
Code:curl -H "Authorization: apikey $ALMA_API_KEY" -H ‘Accept: application/json’ -H ‘Content-Type: application/json’ -X GET ‘https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/9930179940001701’ > 9930179940001701.jsoncurl -H "Authorization: apikey $ALMA_API_KEY" -H ‘Accept: application/json’ -H ‘Content-Type: application/json’ -X PUT ‘https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/9930179940001701’ -d @9930179940001701.json
{"errorsExist":true,"errorList":{"error":[{"errorCode":"402203","errorMessage":"Input parameters mmsId 9930179940001701 is not valid.n(Tracking ID: E01-1808190506-IVA32-AWAE1370915987)"}]},"result":null}The only thing that I’m varying in the two tests is the format. What am I missing?
Jeff
August 19, 2015 at 6:33 am #35618AnonymousInactiveHi Jeff,
Please open a support case – this looks like a real problem.Thanks,
Tamar
Alma API teamAugust 19, 2015 at 1:43 pm #35621AnonymousInactiveWill do… Thanks Tamar.
Incidentally, I also noticed that the JSON-formatted Bib and Holdings objects contain a possibly spurious encoding declaration in the "anies" field. The MARC XML in this field always begin with a
Code:<?xml version="1.0" encoding="UTF-16"?>declaration (regardless of the encoding the user agent requests in the Accept header). However, it looks like the MARC XML string is actually encoded in UTF-8, so the presence of the encoding="UTF-16" declaration is causing strange parsing behavior (some XML parsing libraries throw errors… other silently insert erroneous byte-order markers… etc.)
I’ll open a separate case for this issue.
Jeff
September 22, 2015 at 11:50 pm #35649AnonymousInactivehello,
The standard approach is to use the following
For XML you should use Content-Type: application/xml
For JSON you should use Content-Type: application/jsonHowever the standard doesn’t always work as expected which is why you might need to change the mime type. Try text/json or some other combination and see if that works.
regards,
Brent
PS, Here are some examples from major vendors that I found where the mime-type was very different to what is expected:
curl -I http://ajax.googleapis.com:80/ajax/services/search/web?q=json
> Content-Type: text/html
> Content-Type: text/xmlOctober 12, 2015 at 6:31 am #35661AnonymousInactiveHi,
JSON is not supported in POST/PUT Bib/Holding.
We updated the documentation to indicate this more clearly.Thanks,
TamarMay 3, 2021 at 9:53 am #70703Josh WeismanKeymasterFor those working with Node, the xmldom parser can work with the XML string in the JSON version. See this gist for an example.
For those working with other languages whose XML parsers are more strict and can’t work with the UTF-16 declaration in the JSON version, you can either use the XML version which includes the correct UTF-8 declaration, or remove the UTF-16 declaration from the string manually before parsing.
November 30, 2022 at 4:24 pm #76226Ori MillerKeymasterUpdate:
Following recent requests, we decided to add a header that will fix it:
X-fix-JSON-encoding-declaration: true
For example:
curl ‘https://api…/almaws/v1/bibs/99…?apikey=…&format=json’ -H ‘X-fix-JSON-encoding-declaration: true’
The response will have “UTF-8” instead of “UTF-16” in the XML declaration.
No change in the actual encoding.We continue returning “UTF-16” by default, to be on the safe side (not to break backwards-compatibility in case somehow client-side code is searching for UTF-16), but we assume that adding this header to the request is easier than manipulating the API response before processing it,
Best regards,
Ori
Alma Dev. - AuthorPosts
- You must be logged in to reply to this topic.