0

i would to create script for sending and getting special response from server, i have xml file for sending which contain numbers of IDs i want to send it to server and getting special response from the server like

<Msg>No Record Found</Msg>

or

<Msg>Record Found</Msg>

now I'm using curl like this:

curl -H "Content-Type: text/xml; charset=utf-8" -d @myreq.xml "http://www.example.com/eService.asmx"

now in myreq.xml file contain something like this

 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
 <oma xmlns="http://tempuri.org/">
 <id>303711</id>
 </oma>
 </soap:Body>
 </soap:Envelope>

in id i want to use sequence number start from ( 000001 - 400000 )

and run it to sending to the server

means i would like to send many request and getting the response

anishsane
  • 20,270
  • 5
  • 40
  • 73

2 Answers2

0

If you are going to do anything more than what you asked for to the xml, you should probably use perl or some other programming language for proper xml manipulation. Then you would generate the whole XML file and POST it.

In this simple case, you can put the content of the XML in a here document containing a variable $ID that you can set accordingly, for example like that:

#!/bin/bash
for ID in $(seq 1 3)
do
    DOC=$(cat <<EOM
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <oma xmlns="http://tempuri.org/">
        <id>$ID</id>
    </oma>
</soap:Body>
</soap:Envelope>
EOM
)
    curl -H "Content-Type: text/xml; charset=utf-8" -d "$DOC" "http://www.example.com/eService.asmx"

done
nlu
  • 1,903
  • 14
  • 18
  • thank you for your effort . i run your script it's working fine ,, just i need to add if statement in the end of result which i can get the Users if have IDs or not like: ID 2345 have username ID 2346 Not have username ID 2347 Not have username and the result already contain some info that i can grep it – user3716621 Mar 13 '15 at 13:42
  • So please show, how the response looks. Probably you should not be using `grep` for this as it is not well suited to analyze `xml`. There is a tool called `xmlstarlet`, for example. – nlu Mar 13 '15 at 13:55
0

nlu's answer is good for sending the request. For parsing the response, you don't want to grep: it will be XML, so you should use an XML parsing tool.

This should work:

response=$(curl ... | xmlstarlet sel -t -v '//Msg')

case "$response" in
  "No Record Found") do_something;;
  "Record Found")    do_something_else;;
  *) echo "unexpected response from server for id $ID: $response";;
esac

Find a different tool if you don't like or can't install xmlstarlet.

Community
  • 1
  • 1
glenn jackman
  • 238,783
  • 38
  • 220
  • 352