loentar / ngrest

Fast and easy C++ RESTful WebServices framework
Apache License 2.0
464 stars 93 forks source link

May u be able to extend the rest api to cover ajax as well #45

Closed iot-cloud closed 6 years ago

iot-cloud commented 7 years ago

Hi ngrest gatekeeper

When you are programming using the web interface, rest and ajax are supported.

Is there a possibility to extend the current ngrest to support ajax as well.

What does it take to extend the current ngrest to support ajax? Any pointer is much appreciate?

Thanks and Regards.

loentar commented 7 years ago

I don't understand your question. ngrest is designed to do AJAX. You have to request web resource and get result as JSON (or other format).

iot-cloud commented 7 years ago

Hi Dimtry,

I have tried to use AJAX from ExtJS to NGREST before.

NGREST does not seem throw an exception in regarding the incorrect Context-Type.

Let me work on an example to capture the error and will let you know ASAP.

Regards, Henry

On 6 Nov 2017, at 6:39 pm, Dmitry notifications@github.com wrote:

I don't understand your question. ngrest is designed to do AJAX. You have to request web resource and get result as JSON (or other format).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/loentar/ngrest/issues/45#issuecomment-342068762, or mute the thread https://github.com/notifications/unsubscribe-auth/Abe5YQOm_-LFgJfBHrm0FEzRDGk-aB6Vks5szreugaJpZM4QSpZK.

loentar commented 7 years ago

As I remember there was a problem with ExtJS. If you pass wrong Content-Type to ngrest it will respond with Can't handle content type: xxxx

zlg9folira commented 3 years ago

@loentar I am running examples/crud and need to use get (read), put (update) with requests in python. Get was successful, however, put was not. It throws Can't handle content type:***. I have tried different types and non of them are supported.

Can't handle content type: text/plain
Can't handle content type: text/html
Can't handle content type: text

What content-type should I set when trying put and post ?

headers={"content-type": "text/plain"}
myobj = {'1': 'somevalue'}
x = requests.put(url, headers=headers, data = myobj)
print(x.text)
loentar commented 3 years ago

You should use standard application/json content type.

zlg9folira commented 3 years ago

I see. I just tried that:

headers={"content-type": "application/json"}
myobj = json.dumps({'1': 'somevalue'})
x = requests.put(url, headers=headers, data = myobj)

print(x.text)

and it shows:

Failed to get child data is missing

I can confirm that the object {1} already exists containing some other value. My goal is to update this value and then post new object-value pair. I also tried non-json style myobj = {'rover/camera': 'somevalue'} which throws Unexpected symbol: [r].

Where am I doing wrong here ?

loentar commented 3 years ago

As mentioned in example your request should look like this

      example of request:
      http://server:port/ngrest/examples/data/1
      -- body -----------------------
      {
        "data": "Object #1"
      }

in your case "data" element is missing (you have "1" instead)

For debug start ngrest like this:

NGREST_LOG_LEVEL=TRACE NGREST_LOG_VERBOSITY=ALL ngrest

https://github.com/loentar/ngrest/wiki/ngrest-script

zlg9folira commented 3 years ago

Got it! Now works nicely. Thanks for your quick response.