I found that some APIs cannot be loaded with the syntax loadJSON(). The error message is "Process exited before completing request", but the JSON file works perfectly.
I have made a sketch for demonstration: https://editor.p5js.org/siusoon/sketches/eZWiNI2iS
The api URL: https://api.whatdoestrumpthink.com/api/v1/quotes/personalized?q=
loadJSON then you will find the problem in the console.log Thanks
Welcome! 馃憢 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, be sure to follow the issue template if you haven't already.
This appears to be an issue of request headers which are passed. On a quick glance, I saw that the request made with loadJSON will pass in Content-Type: text/plain along with it. But it isn't actually needed here as Content-Type would only make sense if the request had a body.
$.getJSON does not pass this header.
I verified this using cURL as well
curl https://api.whatdoestrumpthink.com/api/v1/quotes/personalized?q=hello+world
gives
{"message":"Crooked hello world knows nothing about me or my religion","nickname":"Crooked","nlp_attributes":{"pronoun":"he","quote_structure":[["PresentTense","Noun","Preposition","Pronoun","Conjunction","Possessive","Noun"]]}}
whereas
curl https://api.whatdoestrumpthink.com/api/v1/quotes/personalized?q=hello+world -H 'Content-Type: text/plain'
gives
{"errorMessage":"RequestId: cb5ca0ae-2029-4dd2-9189-39eb5a882e72 Process exited before completing request"}
This shows that the error message is not generated by p5. It is generated by the API itself. Maybe the unnecessary header confuses the API.
`
Thanks @akshay-99, what might be the impact if not sending the content-type (just wonder if it may impact other APIs server)? or do you know why it is originally designed in this way? Thanks
I don't know the exact reason. It is probably something with the backend code of the API or something else on the backend configuration. I looked up the error message and found that it is given by AWS lambda when a function running on it exits without completing properly. So there must be something with the code of the API that causes it to crash. It could be that the API sees text/plain and then proceeds to read the request body in and do something with it, but since the body doesn't have anything, it fails and exits.
As far as I know, the convention is to not have a Content-Type header at all if the request does not have a body. And so I am wondering if p5 should check for this too before sending a request, instead of attaching it by default for all requests.
I think it's fine to add a simple check that GET requests doesn't include the Content-Type header. Although the server should not fail in the case where it is present and it probably should be considered a bug on the server side.
Most helpful comment
I think it's fine to add a simple check that
GETrequests doesn't include theContent-Typeheader. Although the server should not fail in the case where it is present and it probably should be considered a bug on the server side.