RE: Getting p:http-request response body when error

Grüß dich Achim,

That works perfectly, thanks a lot !
I missed that assert option which is nevertheless in the spec (https://spec.xproc.org/3.0/steps/#c.http-request)

Xproc 3.0 really rocks :)
Its a pleasure to come back to xproc : 3.0 is really easier to write than Xproc 1.0, it becomes even quite inuitive :)
Really good job guys !

Thanks again,
Greetings from Paris

Matthieu

-----Message d'origine-----
De : Achim Berndzen <achim.berndzen@xml-project.com> 
Envoyé : jeudi 2 novembre 2023 08:37
À : Matthieu RICAUD-DUSSARGET <m.ricaud-dussarget@lefebvre-dalloz.fr>
Cc : xproc-dev@w3.org
Objet : Re: Getting p:http-request response body when error

[Mail EXTERNE]: Vérifiez bien l’expéditeur de l’email avant de cliquer sur des liens ou pièces-jointes!


Bonjour Matthieu,

I think the key for your use case is to turn off the raised XProc error raising. The error is raised because p:http-request has an option "assert" which defaults to '.?status-code lt 400'.

If you change "assert" to "true()" or something more convenient for you, you can access "result" and "report" as the two output ports of p:http-request.

The XProc snippet could be:

 <p:http-request name="sendFile" method="PUT" assert="true()">
        <p:with-option name="href" select="'https://my/api/test'"/>
        <p:with-option name="headers" select="map{'content-type': 'application/json'}"/>  </p:http-request>

<p:choose>
        <p:when test=".?status-code ne 200">
            <p:with-input pipe="report" />
            <!-- An error happened, so let's handle it -->
            <p:wrap-sequence wrapper="error" />
        </p:when>
        <p:otherwise>
            <p:identity>
                <p:with-input><done /></p:with-input>
            </p:identity>
        </p:otherwise>
</p:choose>

Hope this helps.
Greetings from Germany,
Achim

------------------------------------------------
Achim Berndzen
achim.berndzen@xml-project.com

<xml-project /> Achim Berndzen

https://www.xml-project.com


> Am 01.11.2023 um 13:10 schrieb Matthieu RICAUD-DUSSARGET <m.ricaud-dussarget@lefebvre-dalloz.fr>:
>
> Hi,
>
> Back to an xproc dev for a long time, trying xproc 3.0 with  last 
> Morgana release, fun :) But have a problem to get the body of a 400 response error, I only manage to get the headers of the response :
>
> I off course use a try/catch so the pipeline doesn't break on error. But from within the catch I can't get the result the p:http-request which is not accessible.
>
> Let's just explain that the REST API I'm calling give information on the error in the body of the response.
> For example  :
> {
>  "status": 400,
>  "errors": [
>    {
>      "received": "toto",
>      "code": "invalid_enum_value",
>      "options": [
>        "unique",
>        "multiple"
>      ],
>      "path": ["selectionType"],
>      "message": "Invalid enum value. Expected 'unique' | 'multiple', received 'toto'"
>    }
>  ]
> }
> My try/catch looks like this :
>
> <p:try name="try">
>  <p:output port="result">
>    <p:pipe port="result" step="sendFile"/>  </p:output>  
> <p:http-request name="sendFile" method="PUT">
>    <p:with-option name="href" select="'https://my/api/test'"/>
>    <p:with-option name="headers" select="map{'content-type': 
> 'application/json'}"/>  </p:http-request>  <p:catch>
>    <p:output port="result">
>      <p:pipe port="result" step="identity"/>
>    </p:output>
>    <p:identity name="identity"/>
>  </p:catch>
> </p:try>
>
> The output of this step is a c:errors containing a map with headers of the response :
> <c:error code="err:XC0126" name="sendFile" type="p:http-request" href="..." line="56" column="114">
>    <message>Assert expression '.?status-code lt 400' evaluates to false(). See response transcript below:</message>
>    <map xmlns="http://www.w3.org/2005/xpath-functions">
>      <number key="status-code">400</number>
>      <map key="headers">
>        <string key="cache-control">public, max-age=0, must-revalidate</string>
>        <string key="connection">keep-alive</string>
>        <string key="content-type">text/plain;charset=UTF-8</string>
>        <string key="date">Wed, 01 Nov 2023 11:56:15 GMT</string>
>        <string key="server">Vercel</string>
>        <string key="strict-transport-security">max-age=63072000; includeSubDomains; preload</string>
>        <string key="vary">RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url</string>
>        <string key="x-matched-path">/api/[collection]</string>
>        <string key="x-vercel-cache">MISS</string>
>        <string key="x-vercel-execution-region">iad1</string>
>        <string key="x-vercel-id">cdg1::iad1::wsjbl-1698839773942-dcd1ec205f6c</string>
>        <string key="transfer-encoding">chunked</string>
>      </map>
>      <string key="base-uri"> https://my/api/test </string>
>    </map>
>  </c:error>
>
> Is there a way to get the body of the response when the API return an error ?
> I'd like to log the message " Invalid enum value. Expected 'unique' | 
> 'multiple', received 'toto'" from the body
>
> Thanks in advance,
>
> Matthieu Ricaud-Dussarget
> Expert XML
>
> M. 33(0)6 63 25 95 58
> Nouvelle adresse :
> Tour Lefebvre Dalloz
> 10 place des Vosges 92400 Courbevoie
> www.lefebvre-dalloz.fr
>
>
>
>

Received on Thursday, 2 November 2023 12:42:54 UTC