AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
222 stars 20 forks source link

How to extract an <entry> inside <feed> XML response #208

Closed miyamotoh closed 11 months ago

miyamotoh commented 11 months ago

Hi, I admit I'm not very familiar with XPath, but googling around, I came to suspect the below .http code might work and get me the first host ID of many that're returned from the hosts call prior at <feed><entry><id> in the response XML.

###
# @name spps
# @ref hosts
@xpath_ns atom=http://www.w3.org/2005/Atom
@host = {{$xpath.hosts //atom:entry[1]/atom:id}}
GET https://{{hmcsvr}}/rest/api/uom/ManagedSystem/{{host}}/SharedProcessorPool
Accept: application/atom+xml
X-API-Session: {{token}}

However, this {{host}} in the GET request remains empty, despite many variants that I've tried (again, I'm no expert). I hope I'm missing something obvious that an expert can point me to. Thanks!!

AnWeber commented 11 months ago

Your XPath is valid, but you should use a colon instead of $xpath.host

{{$xpath:hosts //atom:entry[1]/atom:id}}

You should see an error in the Output Channel. Maybe I should check if variable exists and give a hint for invalid values. image

Working example

###
# @name hosts
GET https://rss.golem.de/rss.php?ms=open-source&feed=ATOM1.0
Accept: application/atom+xml
###
# @ref hosts
# @name xpath_test
@xpath_ns atom=http://www.w3.org/2005/Atom
@host = {{$xpath:hosts //atom:entry[1]/atom:id}}
{{
  console.info(host)
}}
miyamotoh commented 11 months ago

Ah, great to know that little {{ console.info(host) }} trick to debug. Thanks, @AnWeber! Perhaps these little debug tricks can be added to the Support > Troubleshooting section of your doc?

Deviating from the topic a bit, now that I'm further into the interaction with my API server, I notice that some return, for example, Content-Type: application/foo.bar.wee.json, instead of straight application/json, and the ensuring editor window doesn't give me the "Format Document" menu action (for apparent JSON data). Is there a way you can suggest to fight this?

miyamotoh commented 11 months ago

Oh, never mind. I found @extension in the doc 🎉 , and that's all I needed for the weird Content-Type problem!

###
# @name emondata
# @ref login
# @ref energymon
# @extension json
@xpath_ns atom=http://www.w3.org/2005/Atom
GET {{$xpath:energymon //atom:entry[5]/atom:link/@href}}
Accept: application/json
X-API-Session: {{token}}

With this, the response editor window can "format document" as JSON. All set!

AnWeber commented 11 months ago

Ah, great to know that little {{ console.info(host) }} trick to debug

I fear to spoil a generation of frontend devs, if I say you could debug with console.info :-). But yeah, I will mention it in my docs.

Did already mention it: https://httpyac.github.io/guide/scripting.html#console