API request error

I want to do a API request to display some mathematician’s research publications.

<ul>
    {{ $urlPre := "https://api.archives-ouvertes.fr" }}
    {{ $gistJ := getJSON $urlPre "/search/index/?q=authIdHal_s:marc-arnaudon&wt=json&rows=500&&sort=producedDate_tdate desc" }}
    {{ range $gistJ }}
    <li><a href="{{ .uri_s }}" target="_blank">{{ .label_s }}</a></li>
    {{ end }}
</ul>

But the hugo server return the error:

ERROR 2021/08/19 09:55:13 Failed to get JSON resource “https://api.archives-ouvertes.fr/search/index/?q=authIdHal_s:marc-arnaudon&wt=json&rows=500&&sort=producedDate_tdate desc”: Failed to retrieve remote file: Bad
Request, body: “\n\n400 Bad Request\n\n

Bad Request

\n

Your browser sent a request that this server could not understand.
\n

\n\n”

if somebody can help me…

trought the blockquote coloration of this forum i have seen there was an probleme with the last argument of my url :

&&sort=producedDate_tdate desc

so i have remove it and it don’t return the error anymore but the li stay empty

This is not a Hugo question, but rather how you have to use a particular web API. I suggest that you build a working query outside of your Hugo environment (e.g. with curl or wget) and then put it (the working query) into your code.

Just a hint: There’s still an error in your query since blancs are not permitted in URLs

yes i have seen the problem was the blanc but this is not me that have done this API.
And i thinks the ones who have done it: they had code it with the foots :laughing:

i don’t know how to access to the informations

Be that as it may, it is still not related to Hugo. Il y a une présentation içi, peut-être est-elle utile?

Si je la comprends correctement, il n’y a pas de “search” avec “index”.

1 Like

i don’t understand why in this code:

<ul>
    {{ $urlPre := "https://restcountries.eu" }}
    {{ $respJ := getJSON $urlPre "/rest/v2/region/europe" }}
    {{ range $respJ }}
    <li>{{ .name }}</li>
    {{ end }}
</ul>

why i can’t dislpay the countries names.

Does this

{{$respJ := getJSON (printf "%s%s" $urlPre "/rest/v2/region/europe") }}

work? Or simply passing the whole URL without this “pre” thingy to getJSON?

the two solutions (with printf and with the whole url) didn’t work :cry:
I don’t understand. The API’s response is realy simple and the code to.

i have stop my hugo server and restart it and it work! :face_with_symbols_over_mouth:

I can now fetch the countries’ API but not the HAL one.
I persist: they did it with them foots :rofl: :rofl: :rofl:

The fact remains that: thanks chrillek for your help.

As I said before: make sure that the call works on the command line. If it does not, talk to the people at archives ouvertes. If it does, something in your Hugo setup is not correct.

my call work. You can try it: https://api.archives-ouvertes.fr/search/index/?q=authIdHal_s:marc-arnaudon&fl=authFullName_s,bookTitle_s,producedDate_s,title_s,uri_s,fileMain_s,label_s&wt=json&rows=500

there is something wrong with my Hugo set up

<ul>
    <li>prout prout</li>
    {{ $urlPre := "https://api.archives-ouvertes.fr" }}
    {{ $respJ := getJSON $urlPre "/search/index/?q=authIdHal_s:marc-arnaudon&wt=json&rows=500" }}
    {{ range first 5 $respJ }}
    <li>{{ .label_s }}</li>
    {{ end }}
</ul>

The call itself works in Hugo, too. It is the stuff afterwards that does not. And you do not even say so. If you get an error message (and I’m sure that you do, because I did) post it. Nobody here is a clairvoyant so spare us and yourself the time to hunt around uselessly.

And if you do get an error message, try to understand it and dig deeper. In your case, you were trying to use first with an (JavaScript) object, and Hugo complained bitterly that first got a non-supportet input. That’s the moment to have a look at what you feed it, namely the JSON. It begins like this:

{"response":{"numFound":52,"start":0,"docs":[...

So response is an object that has three properties, namely numFound, start and docs. There simply is no order in the properties of an object, so first can’t work.

It can work with arrays (aka map in Hugo parlance). So you have to get at that part of the repsonse before using first.

{{range first 5 $respJ.response.docs}}

again thanks chillek.
I had found the solution by my self before our last answer.

I have a last question:
in my API’s response there is some array (like: authFullName_s).
How can acces to the differents rows?
because if do: {{ .authFullName_s }} this returne the array with the [ ].

I know how to do it in Javascript and PHP but not with hugo.
I imagine that i have to do loop on it but i don’t know how?
I code since only 1year.
your realy patient ^^

Range again

1 Like

i have done this:

<p>{{ range .authFullName_s }} {{ range .title_s }} {{ .producedDateY_i }}</p>

and it return me this error:

Error: add site dependencies: load resources: loading templates: “C:\wamp64\www\IMB\template-pages-perso-imb-hugo\template-pages-perso-imb-hugo\themes\Hugo-pages-IMB-theme\layouts_default\list.html:14:1”: parse failed: template: _default/list.html:14: unexpected EOF

<div id="cont-publi">
    {{ $urlPre := "https://api.archives-ouvertes.fr" }}
    {{ $respJ := getJSON $urlPre "/search/index/?q=authIdHal_s:marc-arnaudon&fl=authFullName_s,journalTitle_s,journalUrl_s,bookTitle_s,producedDateY_i,title_s,uri_s&wt=json&rows=500&sort=producedDate_tdate%20desc" }}
    {{ $datas := $respJ.response.docs }}
    {{ range $datas }}
    <p>{{ range .authFullName_s }}{{.}}{{ end }} {{ range .title_s }}{{.}}{{ end }} {{ .producedDateY_i }}</p>
    {{ end }}
</div>

thanks a lot to Chrillek to be so patient with a noob like me ^^

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.