Hi,
I have a script that reads formatted text from a Github release description. The text has bullets, some non-ASCII characters, etc. When I look at the string, the special characters are escaped (\r, etc). When I try to write this entire text block to another Github release, it fails with an "error parsing Json" error.
I believe this has to do with either a Conten-type header or UTF encoding, which I'm not fully familiar with.
Is there a straight-forward way in Matlab to read and write formatted text "as-is"?
Thanks.
Joe

8 Kommentare

Walter Roberson
Walter Roberson am 26 Mär. 2021
fread() and fwrite()?
Joe Rustan
Joe Rustan am 26 Mär. 2021
Thanks Walter. I'm not reading content from a file - they are coming (and going) directly into a Github release via the appropriate REST API call. Could I make fread and fwrite work and if so, how? Thanks again.
Joe
Walter Roberson
Walter Roberson am 27 Mär. 2021
Ah, I see what you mean.
I am not familiar with the encoding requirements for that API.
But as a quick test, could you experiment with https://www.mathworks.com/help/matlab/ref/regexptranslate.html ?
Mohammad Sami
Mohammad Sami am 27 Mär. 2021
Assuming you are using webread and webwrite ? If the content type sent by the rest API is application/json, MATLAB webread will automatically process it with jsondecode and return you a struct. Are you getting a struct as the output of webread ?
Joe Rustan
Joe Rustan am 27 Mär. 2021
To be honest, I'm trying to work out this from someone else's code. The relevant lines are:
src_rel_data = webread(src_rel_url);
rel_notes = src_rel_data(rel_index).body)
These notes represent the description box of a Github release. 'body' is one of the fields returned by the Github rest API and the content is in Json format. The read operation works just fine (I've looked at the output), but when writing back rel_notes to a different Github release (using a 'curl' command string with arguments), it fails with the 'error parsing JSON' message. I've tried adding Content-type: application/json, but no luck with that.
Thanks again, hope this provides some clarity.
Joe
Walter Roberson
Walter Roberson am 28 Mär. 2021
Could you speak more of exactly how you are doing the curl ?
Joe Rustan
Joe Rustan am 29 Mär. 2021
Thanks Walter.
The command that is being built is:
C:\Program Files\Git\mingw64\bin\curl.exe" --user "<userid:github credentials>" "https:<github-URL/org/repo/releases -d "tag_name: <name-of-tag>",
"target_commitish: <commit-id>"
"name:<name-of-release",
"body:<rel-body"
I may have missed a few double-quotes and commas in the copy/paste and content replacement, but the actual command works fine with several other repos, so I know it is successful. I also tried adding "Content-type:application/json at the end of the string, but I get the same "error parsing JSON" message.
It is clear the error is happening due to the rel-body containing non-ASCII characters and formatted with bullets, bold text, etc. (which is Github's standard markup language).
Thanks for any expert help I can get. Maybe there is a better way than using the 'curl' command?
Joe

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Mohammad Sami
Mohammad Sami am 29 Mär. 2021

0 Stimmen

I assume this is the api you are using.
On the documentation they show the curl request as follows.
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world/releases \
-d '{"tag_name":"tag_name"}'
I believe you need to form your entire curl request as json string.
From the code I see you are missing the opening and closing {} as well as formatting of your -d argument is not json compliant
For example it should be something like this.
%curl...-d '{"tag_name":"<name-of-tag>","target_commitish":"<commit-id>","name":<name-of-release>","body":"<rel-body>"}'
Or you could can create a struct in matlab with the required parameters then use jsonencode function to form the required json string.
postdata.tag_name = name-of-tag;
postdata.target_commitish = commit-id;
postdata.name = name-of-release;
postdata.body = rel-body;
data = jsonencode(postdata);

Weitere Antworten (0)

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by