Flutter/Dart前端与Go后端POST请求问题及Cookie存储疑问
Hey there! Let's break down your two issues one by one to get you sorted out.
Since your Go backend specifically checks for a Cookie named session, you need to make sure when you send subsequent GET requests, the request header includes the full key-value pair in the correct Cookie format. Here's what to do:
- When you receive the Set-Cookie header from the backend (it’ll look something like
session=your-uuid-here; Path=/; HttpOnly), you can either:- Save the entire
session=your-uuid-herestring directly. Then, when making GET requests, add a header likeCookie: [saved-string]to your request. - Save the key (
session) and value (your-uuid-here) separately. When sending the request, construct the header value assession=your-uuid-hereand add it to theCookieheader.
Either way works, but the critical part is that the backend sees a Cookie namedsessionwith the correct UUID value. Don’t just save the UUID alone—without thesession=prefix, the backend won’t recognize it as the right Cookie.
- Save the entire
浏览器访问的EOF错误问题
Your guess about missing JSON data is spot-on! Here’s why this happens:
- When you visit
localhost:8080directly in a browser, it sends a GET request with no request body. But your login endpoint probably expects a POST request with a JSON payload (like{"username":"your-username","password":"your-password"}). - Go’s JSON parser throws an EOF error when it tries to read a request body that doesn’t exist.
To fix this:
- Use a tool like Postman or curl to send a proper POST request to your login endpoint. Make sure to set the
Content-Typeheader toapplication/jsonand include the required JSON body with your login credentials. - If you want the browser to handle it, you’ll need to create a simple HTML form that sends a POST request with JSON data (or adjust your Go backend to accept GET requests for the login endpoint, though that’s not recommended for security reasons).
内容的提问来源于stack exchange,提问作者DanT29
相关产品推荐
相关产品推荐

