Jak opublikować dane XML za pomocą curl

Chcę publikować dane XML za pomocą cURL. Nie dbam o formularze, jak to zostało powiedziane w Jak zrobić POST request z curl .

Chcę opublikować zawartość XML do jakiegoś serwisu internetowego za pomocą interfejsu wiersza poleceń cURL. Coś w stylu:

curl -H "text/xml" -d "<XmlContainer xmlns='sads'..." http://myapiurl.com/service.svc/

Powyższa próbka nie może jednak zostać przetworzona przez serwis.


Przykład odniesienia w C#:

WebRequest req = HttpWebRequest.Create("http://myapiurl.com/service.svc/");
req.Method = "POST";
req.ContentType = "text/xml";
using(Stream s = req.GetRequestStream())
{
    using (StreamWriter sw = new StreamWriter(s))
        sw.Write(myXMLcontent);
}
using (Stream s = req.GetResponse().GetResponseStream())
{
    using (StreamReader sr = new StreamReader(s))
        MessageBox.Show(sr.ReadToEnd());
}
Author: Community, 2010-01-14

4 answers

-H "text/xml" nie jest prawidłowym nagłówkiem. Musisz podać pełny nagłówek:

-H "Content-Type: text/xml" 
 46
Author: Ben James,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-14 11:10:57

I perfer the following:

cat req.xml | curl -X POST -H 'Content-type: text/xml' -d @- http://www.example.com

Lub

curl -X POST -H 'Content-type: text/xml' -d @req.xml http://www.example.com

Lub

curl -X POST -H 'Content-type: text/xml'  -d '<XML>data</XML>' http://www.example.com 
 10
Author: stones333,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-09 00:15:04

Prostsze jest użycie pliku (req.xml w moim przypadku) z zawartością, którą chcesz wysłać -- Tak:

curl -H "Content-Type: text/xml" -d @req.xml -X POST http://localhost/asdf

Należy również rozważyć użycie typu "application / xml" (różnice wyjaśnione tutaj)

Alternatywnie, bez konieczności wywoływania curla, możesz użyć cat, aby wypluć plik na standardowe wyjście i sprawić, że curl będzie odczytywał ze standardowego wyjścia w następujący sposób:

cat req.xml | curl -H "Content-Type: text/xml" -d @- -X POST http://localhost/asdf

Oba przykłady powinny generować identyczne dane wyjściowe usługi.

 8
Author: dk1844,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:22

Próbowałeś kodować dane url ? cURL może się tym zająć za ciebie:

curl -H "Content-type: text/xml" --data-urlencode "<XmlContainer xmlns='sads'..." http://myapiurl.com/service.svc/
 1
Author: François Feugeas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-04 18:54:34