surrealdb / surrealdb

A scalable, distributed, collaborative, document-graph database, for the realtime web
https://surrealdb.com
Other
26.64k stars 850 forks source link

Bug: How to query SurrealDB /sql endpoint from perl #1657

Closed david6228 closed 1 year ago

david6228 commented 1 year ago

Describe the bug

test perl useragent post request fail.

response {"code":400,"details":"Request problems detected","description":"There is a problem with your request. Refer to the documentation for further information.","information":"There was a problem with the database: Parse error on line 1 at character 0 when parsing '{\"data\":\"SELECT * FROM person WHERE age > 18\"}'"}

Steps to reproduce

perl useragent post request:

my $query={ "data"=>"SELECT * FROM person WHERE age > 18" };
my $ua = Mojo::UserAgent->new;
$ua->post_p("http://root:root@127.0.0.1:8000/sql" =>{  "Accept"=>"application/json", "NS" => "test","DB" =>"test" } => json => $query )
->then( sub ($tx) {
    say $tx->result->body;

})->catch(sub ($err) {
    warn "Connection error: $err";
})->wait;

Expected behaviour

please guide what's problem.

curl -X POST \ -u "root:root" \ -H "NS: myapplication" \ -H "DB: myapplication" \ -H "Accept: application/json" \ -d "SELECT * FROM person WHERE age > 18" \ http://localhost:8000/sql

curl request sucess.

SurrealDB version

surreal-v1.0.0-beta.8.windows-amd64

Contact Details

No response

Is there an existing issue for this?

Code of Conduct

tobiemh commented 1 year ago

Hi @david6228 , I'm not familiar with Perl, but the /sql endpoint accepts text, not JSON. Currently, if I understand it correctly, you are sending an { "data"=>"SELECT * FROM person WHERE age > 18" } object as the body?

david6228 commented 1 year ago

think you respose. I try post a text, but the error is same, may be the problem is that the post request must have a field as key.

my $ua = Mojo::UserAgent->new;
$ua->post_p("http://root:root@127.0.0.1:8000/sql" =>{ "content_type"=>"application/x-www-form-urlencoded","Accept"=>"application/json", "NS" => "test","DB" =>"test" } => form =>{ "data" => "SELECT * FROM person WHERE age > 18" }  )
->then( sub ($tx) {
    say $tx->result->body;

})->catch(sub ($err) {
    warn "Connection error: $err";
})->wait;

{"code":400,"details":"Request problems detected","description":"There is a problem with your request. Refer to the documentation for further information.","information":"There was a problem with the database: Parse error on line 1 at character 0 when parsing 'data=SELECT+*+FROM+person+WHERE+age+%3E+18'"}

david6228 commented 1 year ago

OK, I get the right post request way, think you again.

my $ua = Mojo::UserAgent->new;
$ua->post_p("http://root:root@127.0.0.1:8000/sql" =>{ "Accept"=>"application/json", "NS" => "test","DB" =>"test" } => "SELECT * FROM person WHERE age > 18" )
->then( sub ($tx) {
    say $tx->result->body;

})->catch(sub ($err) {
    warn "Connection error: $err";
})->wait;

[{"time":"41.03\x{00c2}μs","status":"OK","result":[]}]