linzhiming0826 / sanic-restful

Simple framework for creating REST APIs
https://sanic-restful-api.readthedocs.io
MIT License
21 stars 9 forks source link

location error! #6

Closed xkungfu closed 3 years ago

xkungfu commented 3 years ago

server run on port 8000, not 5000.

xkungfu commented 3 years ago

curl http://localhost:8000/todos -d "task=something new" -X POST -v


Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1:8000...
*   Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST /todos HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.75.0
> Accept: */*
> Content-Length: 18
> Content-Type: application/x-www-form-urlencoded
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Content-Length: 673
< Content-Type: text/html; charset=utf-8
< Connection: keep-alive
< Keep-Alive: 5
<
<!DOCTYPE html><html lang=en><meta charset=UTF-8><title>⚠️ 400 — Bad Request</title>
<style>
        html { font-family: sans-serif }
        h2 { color: #888; }
        .tb-wrapper p { margin: 0 }
        .frame-border { margin: 1rem }
        .frame-line > * { padding: 0.3rem 0.6rem }
        .frame-line { margin-bottom: 0.3rem }
        .frame-code { font-size: 16px; padding-left: 4ch }
        .tb-wrapper { border: 1px solid #eee }
        .tb-header { background: #eee; padding: 0.3rem; font-weight: bold }
        .frame-descriptor { background: #e2eafb; font-size: 14px }
    </style>
<h1>⚠️ 400 — Bad Request</h1><p>Failed when parsing body as json
* Connection #0 to host localhost left intact
xkungfu commented 3 years ago

curl http://localhost:8000/todos -d "{\"task\": \"somethin new\"}" -v


*   Trying ::1:8000...
*   Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST /todos HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.75.0
> Accept: */*
> Content-Length: 24
> Content-Type: application/x-www-form-urlencoded
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 201 Created
< Content-type: application/json
< Content-Length: 20
< Connection: keep-alive
< Keep-Alive: 5
<
{
    "task": "s"
}
* Connection #0 to host localhost left intact

finally worked. but what is "s"? why "something new" was changed to "s"?

linzhiming0826 commented 3 years ago

parser.add_argument('task', location='json')

you can try it ,if not set location,code will update values form ('json', 'form', 'args', 'files'),finally the result is "s"

xkungfu commented 3 years ago

thanks.