LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash variable to Curl (https://www.linuxquestions.org/questions/programming-9/bash-variable-to-curl-4175611636/)

ethaniz3d 08-09-2017 04:22 PM

Bash variable to Curl
 
Good Afternoon Everyone,

I am having an issue with a bash script and getting the variables into CURL. I have combed the internet and just can't seem to get it to work. I have a website that I need to pull a log file from for a week timeframe.

Code:

#!/bin/bash
#start date is not date because log file defaults to today's date
StartDate=$(date -d "1 days ago")
EndDate=$(date -d "6 days ago")
echo $StartDate
echo $EndDate

curl --data "StartDate=${StartDate}&EndDate=${EndDate}&xmlfile=xml"

I know I am interfacing with the page correctly because I get a return file in the format of xml and if I input dates under start date and end date, it accepts them and will return the xml file with those dates in it.

One of the examples I have found that I can't seem to get to work is as follows:
Code:

curl --data "$(printf 'StartDate=%q&EndDate=%q&xmlfile=xmlfile' "$StartDate" "$EndDate")"
Can someone please tell me what I'm doing wrong? I just can't seem to get curl to evaluate the variables.

Sefyir 08-09-2017 04:36 PM

Try putting set -xv near the top.

Running it myself, I get this:

Code:

+ curl --data 'StartDate=Tue Aug  8 14:34:08 PDT 2017&EndDate=Thu Aug  3 14:34:08 PDT 2017&xmlfile=xml'
That's quite a strange request. I suspect it's not outputting the information you're expecting. Are you 100% you have configured the date command correctly?

Quote:

if I input dates under start date and end date, it accepts them and will return the xml file with those dates in it.
What are you typing into the url that makes it work?

astrogeek 08-09-2017 04:48 PM

Welcome to LQ!

Quote:

Originally Posted by ethaniz3d (Post 5746490)
I know I am interfacing with the page correctly because I get a return file in the format of xml and if I input dates under start date and end date, it accepts them and will return the xml file with those dates in it.

When you input those dates yourself, when it works, are the dates in the same format as returned by the date command in your script?

michaelk 08-09-2017 06:11 PM

I agree with the others that the default output format from the date command is not what is expected by the web page. Basically --data "StartDate=${StartDate}&EndDate=${EndDate}&xmlfile=xml" is filling out the form as if you actually were entering it using your web browser. The date formats must match. In addition the data needs to be properly encoded i.e If any spaces are required you need to replace them with a %20.


https://curl.haxx.se/docs/httpscript...orms_explained

ethaniz3d 08-10-2017 10:02 AM

I apologize. I need to revise my original post.

Code:

#!/bin/bash
StartDate=$(date -d "1 days ago" +%m-%d-%y)
EndDate=$(date -d "6 days ago" +%m-%d-%y)

on the webpage, the format is m/d/y how would I set it up in that format?

pan64 08-10-2017 10:55 AM

The answer is exactly what you posted.

ethaniz3d 08-10-2017 11:01 AM

So then we know it's not a formatting issue.

pan64 08-10-2017 11:32 AM

It is a formatting issue. And probably something else too. But first you need to fix that formatting, so you need to replace the 2 lines in your script to the two lines from post #5 (StartDate=... and EndDate=...)

ethaniz3d 08-10-2017 11:52 AM

Code:


StartDate=$(date -d "1 days ago" +%m-%d-%y)
EndDate=$(date -d  "6 days ago" +%m-%d-%y)

curl  --data "StartDate=${StartDate}&EndDate=${EndDate}&xmlfile=xmlfile" ip/cgi-bin/logcgi.cgi

this is my current code. I get the xml document but the dates don't reflect the variables.

pan64 08-10-2017 01:55 PM

I don't know what do you mean by that.
Probably you need to swap start and end

ethaniz3d 08-10-2017 03:03 PM

so I revised my code a little bit. thank you for telling me to type set -xv up at the top. Here's my code now and the result of what's being sent.


Code:

#!/bin/bash
set -xv
StartDate=$(date -d "1 days ago" +%m-%d-%y)
EndDate=$(date -d "6 days ago" +%m-%d-%y)

curl --data "StartDate=${StartDate}&EndDate=${EndDate}&xmlfile=xmlfile"

the result is as follows

Code:

curl --data 'StartDate=08-09-17&EndDate=08-04-17&xmlfile=xmlfile'
the result of my xml file has this as the start date and time:


Code:

<log>
<dateStart>1970-01-01T23:59:58-06:00</dateStart>
<dateEnd>1969-12-31T23:59:59-06:00</dateEnd>
<dateGenerated>2017-08-10T19:51:54-05:00</dateGenerated>
</log>


and here's the portion of the site I'm trying to interface with:

Code:

<input name="StartDate" value="08/10/2017" size="15">
<input name="EndDate" value="07/11/2017" size="15">
<input type="submit" name="xmlfile" value="xml file">


astrogeek 08-10-2017 03:53 PM

Quote:

Originally Posted by Sefyir (Post 5746496)
What are you typing into the url that makes it work?

So I'll ask again, please show us an exact example of what does work.

ethaniz3d 08-10-2017 04:13 PM

Code:

#!/bin/bash

curl "StartDate=8/9/2017&EndDate=8/4/2017&xmlfile=xmlfile"

My apologies for being a little slow to your request.

astrogeek 08-10-2017 04:50 PM

Quote:

Originally Posted by ethaniz3d (Post 5746849)
Code:

#!/bin/bash

curl "StartDate=8/9/2017&EndDate=8/4/2017&xmlfile=xmlfile"

My apologies for being a little slow to your request.

No problem, but it is important to respond directly to such questions, sometimes they provide the insight to get directly at a problem.

So, if that works, but this doesn't...

Code:

curl --data 'StartDate=08-09-17&EndDate=08-04-17&xmlfile=xmlfile'
What happens if you change your date format string to produce a date string that matches the working example?

It is probably a matter that the target script is very picky about what you send to it.

ethaniz3d 08-10-2017 05:07 PM

so now I have the following


Code:

#!/bin/bash
set -xv
StartDate=$(date -d "1 days ago" +%m/%d/%y)
EndDate=$(date -d  "6 days ago" +%m/%d/%y)


curl  --data "StartDate=${StartDate}&EndDate=${EndDate}&xmlfile=xmlfile"


this is the result:
Code:

curl --data 'StartDate=08/09/17&EndDate=08/04/17&xmlfile=xmlfile'
How do I get the full year?


All times are GMT -5. The time now is 07:08 PM.