Jump to content

POST request using cURL command line client


pamela.curts

Recommended Posts

Hi everyone. I'm new in this forums, thanks for reading.

 

I've tried the prestashop webservice using the php example script located in the documentation and i've read it all. It's really easy and works great.

 

But now i'm trying to use the webservice via cURL command line client. i managed to make GET, PUT, and DELETE requests but when i try to make a POST request i get a HTTP/1.1 500 Internal Server Error.

 

This is the command i launch:

 

curl -v -X POST -H "Content-type:text/xml" --data-urlencode @cnew.xml "http://JXDLSL4MTV0GLFFNIMC4H35L4DV93IYP:@192.168.1.3/prestashop/api/customers/"

 

I use -v for verbose. I get this:

 

* About to connect() to 192.168.1.3 port 80 (#0)
*   Trying 192.168.1.3... connected
* Connected to 192.168.1.3 (192.168.1.3) port 80 (#0)
* Server auth using Basic with user 'JXDLSL4MTV0GLFFNIMC4H35L4DV93IYP'
> POST /prestashop/api/customers/ HTTP/1.1
> Authorization: Basic SlhETFNMNE1UVjBHTEZGTklNQzRIMzVMNERWOTNJWVA6
> User-Agent: curl/7.21.7 (i386-pc-win32) libcurl/7.21.7 OpenSSL/0.9.8r zlib/1.2
.5 libidn/1.18 libssh2/1.2.8 librtmp/2.3
> Host: 192.168.1.3
> Accept: */*
> Content-type: text/xml
> Content-Length: 989
>
< HTTP/1.1 500 Internal Server Error
< Date: Fri, 22 Jun 2012 08:59:59 GMT
< Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl
/2.0.4 Perl/v5.10.1
< X-Powered-By: PrestaShop Webservice
< Access-Time: 1340355599
< PSWS-Version: 1.4.7.0
< Execution-Time: 0.016
< Content-Length: 241
< Connection: close
< Content-Type: text/xml;charset=utf-8
<
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<message><![CDATA[internal error. To see this error please display the PHP error
s.]]></message>
</error>
</errors>
</prestashop>
* Closing connection #0

 

The content of cnew.xml file is a new customer:

 

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer>
<id></id>
<id_default_group></id_default_group>
<newsletter_date_add></newsletter_date_add>
<ip_registration_newsletter></ip_registration_newsletter>
<last_passwd_gen></last_passwd_gen>
<secure_key></secure_key>
<deleted></deleted>
<passwd></passwd>
<lastname>Ñoréz</lastname>
<firstname>Trinitarió Fúlgencio</firstname>
<email>[email protected]</email>
<note></note>
<id_gender></id_gender>
<birthday></birthday>
<newsletter></newsletter>
<optin></optin>
<active>1</active>
<is_guest></is_guest>
</customer>
</prestashop>

 

If I use --trace-ascii instead of -v:

 

curl --trace-ascii err.txt -X POST -H "Content-type:text/xml" --data-urlencode @cnew.xml "http://JXDLSL4MTV0GLFFNIMC4H35L4DV93IYP:@192.168.1.3/prestashop/api/customers/"

 

I get a err.txt file with more information. This is its content:

 

== Info: About to connect() to 192.168.1.3 port 80 (#0)
== Info:   Trying 192.168.1.3... == Info: connected
== Info: Connected to 192.168.1.3 (192.168.1.3) port 80 (#0)
== Info: Server auth using Basic with user 'JXDLSL4MTV0GLFFNIMC4H35L4DV93IYP'
=> Send header, 308 bytes (0x134)
0000: POST /prestashop/api/customers/ HTTP/1.1
002a: Authorization: Basic SlhETFNMNE1UVjBHTEZGTklNQzRIMzVMNERWOTNJWVA
006a: 6
006d: User-Agent: curl/7.21.7 (i386-pc-win32) libcurl/7.21.7 OpenSSL/0
00ad: .9.8r zlib/1.2.5 libidn/1.18 libssh2/1.2.8 librtmp/2.3
00e5: Host: 192.168.1.3
00f8: Accept: */*
0105: Content-type: text/xml
011d: Content-Length: 989
0132:
=> Send data, 989 bytes (0x3dd)
0000: %3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0
0040: A%3Cprestashop%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F19
0080: 99%2Fxlink%22%3E%0A%3Ccustomer%3E%0A%09%3Cid%3E%3C%2Fid%3E%0A%09
00c0: %3Cid_default_group%3E%3C%2Fid_default_group%3E%0A%09%3Cnewslett
0100: er_date_add%3E%3C%2Fnewsletter_date_add%3E%0A%09%3Cip_registrati
0140: on_newsletter%3E%3C%2Fip_registration_newsletter%3E%0A%09%3Clast
0180: _passwd_gen%3E%3C%2Flast_passwd_gen%3E%0A%09%3Csecure_key%3E%3C%
01c0: 2Fsecure_key%3E%0A%09%3Cdeleted%3E%3C%2Fdeleted%3E%0A%09%3Cpassw
0200: d%3E%3C%2Fpasswd%3E%0A%09%3Clastname%3E%C3%91or%C3%A9z%3C%2Flast
0240: name%3E%0A%09%3Cfirstname%3ETrinitari%C3%B3%20F%C3%BAlgencio%3C%
0280: 2Ffirstname%3E%0A%09%3Cemail%3Etrini%40nor.ez%3C%2Femail%3E%0A%0
02c0: 9%3Cnote%3E%3C%2Fnote%3E%0A%09%3Cid_gender%3E%3C%2Fid_gender%3E%
0300: 0A%09%3Cbirthday%3E%3C%2Fbirthday%3E%0A%09%3Cnewsletter%3E%3C%2F
0340: newsletter%3E%0A%09%3Coptin%3E%3C%2Foptin%3E%0A%09%3Cactive%3E1%
0380: 3C%2Factive%3E%0A%09%3Cis_guest%3E%3C%2Fis_guest%3E%0A%3C%2Fcust
03c0: omer%3E%0A%3C%2Fprestashop%3E
<= Recv header, 36 bytes (0x24)
0000: HTTP/1.1 500 Internal Server Error
<= Recv header, 37 bytes (0x25)
0000: Date: Fri, 22 Jun 2012 08:32:26 GMT
<= Recv header, 99 bytes (0x63)
0000: Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/
0040: 5.3.8 mod_perl/2.0.4 Perl/v5.10.1
<= Recv header, 37 bytes (0x25)
0000: X-Powered-By: PrestaShop Webservice
<= Recv header, 25 bytes (0x19)
0000: Access-Time: 1340353946
<= Recv header, 23 bytes (0x17)
0000: PSWS-Version: 1.4.7.0
<= Recv header, 23 bytes (0x17)
0000: Execution-Time: 0.015
<= Recv header, 21 bytes (0x15)
0000: Content-Length: 241
<= Recv header, 19 bytes (0x13)
0000: Connection: close
<= Recv header, 38 bytes (0x26)
0000: Content-Type: text/xml;charset=utf-8
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 241 bytes (0xf1)
0000: <?xml version="1.0" encoding="UTF-8"?>.<prestashop xmlns:xlink="
0040: http://www.w3.org/1999/xlink">.<errors>.<error>.<message><![CDAT
0080: A[internal error. To see this error please display the PHP error
00c0: s.]]></message>.</error>.</errors>.</prestashop>.
== Info: Closing connection #0

 

I've tried several "-H" options (headers) like: -H "Content-type:text/xml" and -H "Content-type:application/x-www-form-urlencoded" but I always get HTTP/1.1 500 Internal Server Error :(

 

Can anybody help me PLEASE?

Thanks in advance!!!

 

--

Pamela Curts

Link to comment
Share on other sites

Hi again. If could help. i've had this conversation in #curl IRC channel at irc.freenode.net:

 

<B4gder> hi
<pamela> i need to convert a curl_exec() PHP call to command line using cURL cli, can anybody help me please?
<B4gder> so tell us what you've got and we might be able to help
<pamela> ok thanks. i'm going to paste some code in pastebin
<pamela> it's about prestashop webservice (prestashop is a php cms for ecommerce and offers a RESTfull interface which i need to use via command line)
<pamela> i'm trying to do a POST request to send this XML: http://pastebin.com/cvDqB8Qu
<pamela> the command i'm using is: curl -v -X POST -H "Content-type:text/xml" --data-urlencode @cnew.xml "http://JXDLSL4MTV0GLFFNIMC4H35L4DV93IYP:@192.168.1.3/prestashop/api/customers/"
<pamela> well, more information about this (what i get using that command, etc) is here: http://www.prestashop.com/forums/topic/175128-post-request-using-curl-command-line-client/
<B4gder> curl -d @xmlfile -H "Content-type: text/xml" [url]
<pamela> now i'm viewing some php excample scripts
<pamela> for make a POST request to prestashop webservice
<pamela> this is the code that make the POST request: http://pastebin.com/B6bRz672ç
<pamela> sorry: http://pastebin.com/B6bRz672
<pamela> this code calls a add() method of the class PrestaShopWebService. This class is provided for Prestashop and here it is: http://pastebin.com/eqqFrmAY
<B4gder> optionally if you really need your newlines etc:
<pamela> B4gder, that is the info I can give. If you need more info askme, and thanks for help me...
<B4gder> curl --data-binary @xmlfile -H "Content-type: text/xml" [url]
<pamela> ok, i,m going to try that commands
<B4gder> the most important thing is that you need to understand
<B4gder> 1) what to send and 2) how to make curl send that
<pamela> ok, i want to send a xml text file (utf8 encoded) and via POST, using text/xml header but url-encoded
<pamela> btw i've tried that command (with -d and --binary-data params) and i get the same error: HTTP/1.1 500 Internal Server Error
<B4gder> url encoded XML, really?
<B4gder> that would be highly unusual
<pamela> yes because my xml has accents, spaces, etc. and i need to auto-encode that stuff
<B4gder> ?
<B4gder> you can't just encode things at your choice
<pamela> well, yes, i can 
<B4gder> well yes you can, but it doesn't work that way
<pamela> ok so, it's better to manual encode the xml isn't it?
<pamela> oh ok
<B4gder> why would it be encoded at all?
<B4gder> it is xml
<pamela> if i don't encode spaces i'll get "%20" strings, dont it?
<B4gder> a space, as in byte 0x20 will remain 0x20 if not encoded
<pamela> (sorry for my english, if i'm expressing badly alerts me if you don't understand something)
<pamela> so i don't need to url-encode the data before sending it?
<B4gder> most likely you will break things if you do
<pamela> ok, i've changed my command to: curl -v -X POST -H "Content-type:text/xml" -d @cnew.xml "http://JXDLSL4MTV0GLFFNIMC4H35L4DV93IYP:@192.168.1.3/prestashop/api/customers/"
<pamela> i keep getting the "500 Internal Server Error"
<pamela> B4gder, so the command i'm using is well? may be the problem is with prestashop webservice?
<B4gder> you need to know exactly how the data you want to send should be formatted
<pamela> ok
<pamela> if i use the php script to do a POST request
<pamela> i get this after the successfully request:
<pamela> http://pastebin.com/xnShsU5S
<B4gder> and if you compare the sent requests, how do they differ?
<B4gder> content-type is one it looks like
<pamela> if i use command line, the response i get is here (using --trace-ascii): http://pastebin.com/cqZYGv6g
<B4gder> and if you compare the sent *requests*
<pamela> using PHP --> Content-Type: application/x-www-form-urlencoded
<pamela> using command --> Content-type:text/xml
<B4gder> so why do you enforce that header?
<pamela> using PHP --> Content-Length: 629
<pamela> using command: Content-Length: 192
<B4gder> then you're hardly sending the same xml, are you?
<pamela> but i tried too using -H "Content-Type:application/x-www-form-urlencoded" and don't work neither
<B4gder> that's the default content-type with post
<pamela> i think i'm sending same xml but it seems not...
<pamela> so, it's the XML i sent via command line. it's incorrect

 

Help please!!

Link to comment
Share on other sites

Hi.

you could try to add "xml=" and the start of your xml file.

xml=<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer>
<id></id>
...
...

 

And this is my working curl command:

curl -X POST -u 'JXDLSL4MTVOGLFFNIMC4H35L4DV93IYP:' -d @test.xml 'http://192.168.1.3/api/customers/'

Edited by down999 (see edit history)
Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...