Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to strip down cURL return to just the asked for field data?

Posted on 2015-01-31
14
Medium Priority
?
164 Views
Last Modified: 2015-02-02
This cURL script works fine for accessing my API:

curl -X GET -H "ACCEPT: application/xml" -H "X-AUTH-TOKEN: cee8d8a1afd5921dc1efe1817"  "https://api.niftywebsite.com/?opts=APNMB&apps=13629198"  -o out.txt

APNMB is a field in the database.

What I get back is:

<?xml version="1.0" encoding="utf-8"?>
<response>
      <entity>
            <APNMB>13629198</APNMB>
      </entity>
</response>

All I want back is the app number, which is 13629198

Other than writing to a temp file and stripping it out, is there a way directly in cURL to just get the app number returned?
0
Comment
Question by:Mike Caldwell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 40

Assisted Solution

by:noci
noci earned 1332 total points
ID: 40582521
No,

curl is meant to do just the transfer parts. I is not meant to handle content.
otoh, by running:

curl <yourquery> | parse-script

where parse script can be:
---8<---
#!/bin/bash
grep 'APNMB' | sed -e 's/^[^>]*>//' -e 's/<.*$//'
---8<---

or just:
curl <yourquery> |grep 'APNMB' | sed -e 's/^[^>]*>//' -e 's/<.*$//'
0
 
LVL 33

Assisted Solution

by:shalomc
shalomc earned 668 total points
ID: 40582536
No, there is no way for curl to do it directly.
You don't have to write to a temp file, though. You can pipe it to an XML processing script/program/utility as input.
Just don't use the -o switch.

curl -X GET -H "ACCEPT: application/xml" -H "X-AUTH-TOKEN: cee8d8a1afd5921dc1efe1817"  "https://api.niftywebsite.com/?opts=APNMB&apps=13629198"      | some-xml-processor
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40582896
noci, I added |grep 'APNMB' | sed -e 's/^[^>]*>//' -e 's/<.*$//'  after my full query, and I got an error message of "grep is not an internal or external command"
0
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40582928
OK, new learning!  I did not realize that "|" meant new line.  So I redid it, and I still get that error message.
0
 
LVL 40

Expert Comment

by:noci
ID: 40583509
| is NOT newline, but send output from command before | to input of command after |.

It does mean end of a command part.  And all input needs to be in ONE line.
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40583880
That is how I have it, and I still get the whole structure, not just the part I want,.  I think I'm hoping for something that cURL won't do.
0
 
LVL 40

Accepted Solution

by:
noci earned 1332 total points
ID: 40584371
curl cannot parse xml...

curl -X GET -H "ACCEPT: application/xml" -H "X-AUTH-TOKEN: cee8d8a1afd5921dc1efe1817"  "https://api.niftywebsite.com/?opts=APNMB&apps=13629198" | grep 'APNMB' | sed -e 's/^[^>]*>//' -e 's/<.*$//'

All on one line should only produce the number:
13629198

note: the " en ' characters ARE important.

if not then please produce wat IS shown as an answer including error messages etc.
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40584501
Got an error message that grep is not a recognized internal or external program.  Windows cURL seems to require double quotes, so I copied and pasted what you posted, then tried again with double quotes like this:


curl -X GET -H "ACCEPT: application/xml" -H "X-AUTH-TOKEN: ceed8d98159b218dc1efe1817" "https://api.niftysite.com/v3/appSearch/?opts=APBNMB&apps=13629198" | grep "APNMB" | sed -e "s/^[^>]*>//" -e "s/<.*$//" -o EE.txt
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40584502
To be clear: nothing shown since it did not run.
0
 
LVL 33

Expert Comment

by:shalomc
ID: 40584524
grep and sed are unix/linux commands, so if you run it on windows you get nothing.

Here is a XSLT transformation to retrieve your value:
<?xml version="1.0"?>   
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
<xsl:template match="APNMB">       
<xsl:value-of select="."/>   
</xsl:template>                    
</xsl:stylesheet>                

Open in new window


All you need now is an XSLT processor.
Try this https://sourceforge.net/projects/saxon/files/Saxon-HE/

I will post shortly more detailed instructions..
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40584587
Please do; I did not see anything in the download that hinted at installation.
0
 
LVL 40

Expert Comment

by:noci
ID: 40584824
old_mike your are correct, cURL is a transfer program so you can access remote systems from a command line or a script... so can't handle content... like html, text, ....  just transfer methods like
HTTP, FTP, ... so how should it hint about content. You can build complete scripts (except for javascript handling) in a shell language, (or php, or perl) to simulate clicking through websites by filling out webforms, GET-ting info, POST-ing info etc. all while managing the cookies & other HTTP headers.
The tool was developed on Unix and ported to various platforms. On Unix (Linux too) there are a lot of programs with a specific defined task. so cURL is a transfer tool, grep is a search tool (like Find on windows, a tad more powerful though), and sed is a kind of editor.

Also getting the info using an XSLT transform will be more exact. The grep / sed method works for a known case with a simple value like your problem.

BTW. For unix tooling under windows checkout cygwin. ( http://cygwin.org/ )
curl & bash can be part of that.

BTW2, it helps to also describe the platform in the question.
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40584916
Yeah, I see that I should have mentioned Windows up front.  Noci, this project is getting way beyond me from a time standpoint.  My partner is my son, and he is expert in all this, just doing something else so I thought I would make some very simple reports from the tool.  By the time I get done, he'll have it done, so I think I'll thank everyone and stop here.  I may still try to access the API with a VBScript, which I have done a lot of.
0
 
LVL 33

Expert Comment

by:shalomc
ID: 40584974
Installing Saxon:
https://sourceforge.net/projects/saxon/files/Saxon-HE/9.6/
Get the "setup" exe file. It will create a folder in "program files". Add the bin folder you your path.

Running XSLT from the command line:
http://www.saxonica.com/html/documentation/using-xsl/commandline.html

Save the XSLT into a file, for example old_mike.xslt and run
Transform -s:xmlfile.xml  -xsl:old_mike.xslt -strip:all

Add the -o:outputfile switch if you need it in a file.

Here is an updated XSLT file.  
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">  
<xsl:output method="text"/>
<xsl:template match="APNMB">       
<xsl:value-of select="."/>   
</xsl:template>                    
</xsl:stylesheet>                

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question