Jump to content

How send a PUT / POST to Webservice API


Matt_

Recommended Posts

Hi

How can I send a PUT / POST HTTP request, without PHP?

I have used PHP a heap, but now I writing for a smartphone.  (Using Swift, Java and was C#).  (If asked, I can show the swift code that I use.)

GET is simple, you can also just put the URL in a web browser to get a response.  I have several functions to get customers, addresses, products, etc. and these work well.  (I have many object models - like a database table structure - that store the data.). (Because JSON is much easier to decode/encode, I apply "&output_format=JSON".  If only I could also PUT / POST JSON...)

However when it comes to making a PUT / POST (and probably DELETE in the future...) request I keep getting errors.

(I know this is probably the wrong way: but I created a module where you can POST the customer's email and password.  The template would provide either the complete customer record - if validated - or "BAD" if no record with matching details was found.  The request content is like "email=xxx&passwd=yyy" (application/x-www-form-urlencoded) and is translated to UTF-8; then the Content-Length is set.)

Using Fiddler, I can view the exact transaction (request & response):  [see: https://ibb.co/kC0QTy]

When I attempt a PUT request and I send XML (wish I could send JSON instead):

    PUT http://example.com/api/customers?ws_ket=ABC..XYZ
    Content-Type: application/xml; charset=utf-8
    Accept: */*
    ...

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

It responds with:

    HTTP/1.1 500 Internal Server Error
    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>
    <code><![CDATA[127]]></code>
    <message><![CDATA[XML error : String could not be parsed as XML
XML length : 0
Original XML : ]]></message>
    </error>
    </errors>
    </prestashop>

And much the same for a POST.

What do I need to send XML, using PUT and POST?

 

Link to comment
Share on other sites

  • 2 weeks later...

I have further tests to go...
but the problem seems to be that I added an extra line (and it's closer).  Specifically `"<customers>"`

One test worked with a REST client application (by WizTools.org) and its respond was as excepted.

I assume that the extra wrapper caused the invalid XML error...

testing properly

UPDATE

I successfully created a new customer!  Found in the request, the data shouldn't be wrapped in CDATA (ie. <abc><![CDATA[input_data]]></abc> where I want insert input_data in abc)

now to PUT...

Edited by Matt_
more information (see edit history)
Link to comment
Share on other sites

Can someone help me?

I'm stuck on PUT, where I update a record and send it back.

With POST - to insert a new record, I first GET the blank schema insert my details (in plain text) then POST the construction to the same URL and it does the magic and sends a response of the entire record with formatted details including the assigned id.  Works fine!

But with PUT - to update the details, I. first GET the record (by id) change as is needed then I PUT the XML back to the same URL.  So it is similar to above, but it fails:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[127]]></code>
<message><![CDATA[XML error : String could not be parsed as XML
XML length : 0
Original XML : ]]></message>
</error>
</errors>
</prestashop>

How is the PUT different, what extra steps are needed?

Link to comment
Share on other sites

  • 7 months later...

Hi

I suspect my host is blocking PUT requests.

Using a REST client I can test a variety of requests, only PUT is not working.  I tried with "text/xml", "application/xml" and some other formats - no difference!

Are you having a problem with POST?

 

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...
On 6/28/2018 at 5:43 PM, Matt_ said:

Can someone help me?

I'm stuck on PUT, where I update a record and send it back.

With POST - to insert a new record, I first GET the blank schema insert my details (in plain text) then POST the construction to the same URL and it does the magic and sends a response of the entire record with formatted details including the assigned id.  Works fine!

But with PUT - to update the details, I. first GET the record (by id) change as is needed then I PUT the XML back to the same URL.  So it is similar to above, but it fails:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[127]]></code>
<message><![CDATA[XML error : String could not be parsed as XML
XML length : 0
Original XML : ]]></message>
</error>
</errors>
</prestashop>

How is the PUT different, what extra steps are needed?

Getting a register and editing or getting blank fields and filling, and after trying POST or PUT is not working in my case.

Can you give me more details about this procedure?? I'm so interested on it and i've spend all day researching for it with no results...

 

Thanks!

Link to comment
Share on other sites

Hi

There's a few people with comments, but you seem to quote my post...

I am unable to PUT correctly, so I'm not the best to advise.  But I need to know where your problem is and I'll help where I can.

Please provide more information about your issue.

 

Link to comment
Share on other sites

10 minutes ago, Matt_ said:

Hi

There's a few people with comments, but you seem to quote my post...

I am unable to PUT correctly, so I'm not the best to advise.  But I need to know where your problem is and I'll help where I can.

Please provide more information about your issue.

 

I quoted you given is your post and at least you reached to make a successfully POST.

In postman I following your indications, for example in Customer context:

  1. I make a GET request to /customers with schema: blank parameter to get the structure.
  2. I fill all fields as possible.
  3. I paste it to the body of the request and I make POST to /customers with that XML data with filled fields.
  4. It returns an array of customers but mine new customer IS NOT BEING CREATED.
Link to comment
Share on other sites

Hi

Can you view the creation XML?  Can you view the XML from ps reply?

Ensure the XML that you post doesn't contain the encapsulation "CDATA" and ensure the XML is valid code.  The data should abide by the SCHEMA synopsis.

Try to POST the same data as an existing user - bar an extra few letters...

I have a different computer now and the project is in storage but I can remember most...

I used an application called something REST client and it was  useful to connect and see the XML code.

Link to comment
Share on other sites

Yes, this is the empty XML that I get from the API, and I filled some of the fields.

When I do POST it returns the customers list without adding that new one...

 

<?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>
        <id_lang>3</id_lang>
        <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>12313123</passwd>
        <lastname>becquel</lastname>
        <firstname>becquel</firstname>
        <email>[email protected]</email>
        <id_gender>1</id_gender>
        <birthday></birthday>
        <newsletter></newsletter>
        <optin></optin>
        <website></website>
        <company></company>
        <siret></siret>
        <ape></ape>
        <outstanding_allow_amount></outstanding_allow_amount>
        <show_public_prices></show_public_prices>
        <id_risk></id_risk>
        <max_payment_days></max_payment_days>
        <active></active>
        <note></note>
        <is_guest></is_guest>
        <id_shop>1</id_shop>
        <id_shop_group>1</id_shop_group>
        <date_add></date_add>
        <date_upd></date_upd>
        <reset_password_token></reset_password_token>
        <reset_password_validity></reset_password_validity>
        <associations>
            <groups>
                <group>
                    <id>1</id>
                </group>
            </groups>
        </associations>
    </customer>
</prestashop>

 

 

Have you got something in mind?

Link to comment
Share on other sites

Hi

Using the XML you gave, it return '201 Created'  - on my installation.

Then when I did a GET for the latest customer it had details for today with firstname as becquel, so it seemed to have worked.

If you don't get I assume there's some thing disabled by your host.

Link to comment
Share on other sites

On 2/15/2019 at 3:19 PM, Matt_ said:

Hi

I suspect my host is blocking PUT requests.

Using a REST client I can test a variety of requests, only PUT is not working.  I tried with "text/xml", "application/xml" and some other formats - no difference!

Are you having a problem with POST?

 

I have seen this before. Some hosting providers consider GET and POST are the standard, and blacklist PUT, DELETE, PATCH HTTP calls as they consider "only hackers use this".

Check it thoroughly, and if confirmed you might consider changing the host provider ...

Link to comment
Share on other sites

17 hours ago, Matt_ said:

Hi

Using the XML you gave, it return '201 Created'  - on my installation.

Then when I did a GET for the latest customer it had details for today with firstname as becquel, so it seemed to have worked.

If you don't get I assume there's some thing disabled by your host.

Seriously?

So I don't know where to start finding. I've enabled all CRUD methods for customer in webservices section in prestashop...

Does someone know what could be happening or been in the same situation?? 

 

Thanks in advance

Link to comment
Share on other sites

17 hours ago, ksaan said:

I have seen this before. Some hosting providers consider GET and POST are the standard, and blacklist PUT, DELETE, PATCH HTTP calls as they consider "only hackers use this".

Check it thoroughly, and if confirmed you might consider changing the host provider ...

I think my troubles could be related with it...

I'm going to have a look and maybe I'll find a solution for me and @Matt_

Link to comment
Share on other sites

On 5/18/2019 at 10:39 AM, Becquel said:

I think my troubles could be related with it...

I'm going to have a look and maybe I'll find a solution for me and @Matt_

Well, at the end is related with it.

 

As I can see in acces.log from apache, when i make a POST request apache is converting it to a GET request. 

I trying to make some changes in .htaccess from prestashop's folder. If I reach something i'll share with you and i guess Matt's PUT troubles are related with the same issue.

 

Link to comment
Share on other sites

Be aware that apache also have its own configuration file modified and maintained y-by your hosting provider. I dont know whether .htaccess overrides the apache config settings or it's the other way around.

Have you asked the hosting provider directly ? Maybe a question in an email can provide you all the answers you need.

Link to comment
Share on other sites

FIXED, It was related with http and https redirections. Postman doesn't manage this as I expected and I had to put https://... instead of the domain without specifying the protocol.

Asking to my hosting provider had no sense given I have presta in a VPS, so...

 

Anyway actually I got it working like a charm even with PUT requests.

Matt_ let me know if you want to see any config or whatever you need to fix your problem.

 

Thanks both @ksaan and @Matt ;)

 

  • Like 1
Link to comment
Share on other sites

That's great Becquel!

 Unfortunately for me, we don't have a SSL certificate on our site.

In a client (insomnia), when I used https, it gave an error.  Although my host has an overall certificate, when I used https://mysite.their_address, it just returns my site and ignore the PUT request.

 

Link to comment
Share on other sites

Yesterday looking for a solution for my troubles I've found a lot of info about apache(not sure if you're using nginx) turning all requests into GET. Could be related with it and they were talking about .htaccess in prestashop's root directory. 

 

Which OS and webserver(nginx or apache are you using)? Let us try to give a help

Link to comment
Share on other sites

Their OS is Debian but I'm using shared hosting and they probably have the final says about configuration.  Although there's a section where I can select the PHP version and it seems I can edit the php.ini...

 

They're using CGI/FastCGI,  $_SERVER['SERVER_SOFTWARE'] says Apache/2.  Whether they use Apache or Nginx, it most probably call PHP-fpm to execute any PHP. 

I'm still dubious if I can actually make changes to allow PUT, but I have hope.

 

Link to comment
Share on other sites

Well, as I can see in the first comment of this post I think the problem is in your XML format.

Apache is not blocking your request given Apache is not checking your request for XML errors, this is done by your webservices endpoint.

So let me tell my procedure to make a POST / PUT and try to reproduce in your server:

1. I make POST to the url https://domain.com/shop/api/customers?ws_key=ABCDEFGHIJKLMNOPQRST123123123

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <customer>
        <id_default_group></id_default_group>
        <id_lang></id_lang>
        <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>123456123</passwd>
        <lastname>Unknown</lastname>
        <firstname>Matthew</firstname>
        <email>[email protected]</email>
        <id_gender></id_gender>
        <birthday></birthday>
        <newsletter></newsletter>
        <optin></optin>
        <website></website>
        <company></company>
        <siret></siret>
        <ape></ape>
        <outstanding_allow_amount></outstanding_allow_amount>
        <show_public_prices></show_public_prices>
        <id_risk></id_risk>
        <max_payment_days></max_payment_days>
        <active></active>
        <note></note>
        <is_guest></is_guest>
        <id_shop></id_shop>
        <id_shop_group></id_shop_group>
        <date_add></date_add>
        <date_upd></date_upd>
        <reset_password_token></reset_password_token>
        <reset_password_validity></reset_password_validity>
        <associations>
            <groups>
                <group>
                    <id></id>
                </group>
            </groups>
        </associations>
    </customer>
</prestashop>

2. I make PUT to the url https://domain.com/shop/api/customers/17?ws_key=ABCDEFGHIJKLMNOPQRST123123123 where you have to change the /17 for the id of your new customer.

After that add a <id>17</id> at the xml structure and thats all. 

I also have to say that since I enabled multishop now PUT requests are updating data but disabling the customer given not appear in the backend, for sure is related with some field like active or disabled... 

Anyway, after adding the id in the URL and in the XML. the body of the request will be the following:
 

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <customer>
    	<id>17</id>
        <id_default_group></id_default_group>
        <id_lang></id_lang>
        <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>123456789</passwd>
        <lastname>Presta</lastname>
        <firstname>Presta</firstname>
        <email>[email protected]</email>
        <id_gender></id_gender>
        <birthday></birthday>
        <newsletter></newsletter>
        <optin></optin>
        <website></website>
        <company></company>
        <siret></siret>
        <ape></ape>
        <outstanding_allow_amount></outstanding_allow_amount>
        <show_public_prices></show_public_prices>
        <id_risk></id_risk>
        <max_payment_days></max_payment_days>
        <active></active>
        <note></note>
        <is_guest></is_guest>
        <id_shop></id_shop>
        <id_shop_group></id_shop_group>
        <date_add></date_add>
        <date_upd></date_upd>
        <reset_password_token></reset_password_token>
        <reset_password_validity></reset_password_validity>
        <associations>
            <groups>
                <group>
                    <id></id>
                </group>
            </groups>
        </associations>
    </customer>
</prestashop>

 

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

  • 8 months 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...