简体   繁体   English

Go HTTP Post Request 没有为 Ruby On Rails API 正确格式化数据

[英]Go HTTP Post Request not formatting data properly for Ruby On Rails API

I've built a client in Go to interact with my Rails API.我在 Go 中构建了一个客户端来与我的 Rails API 交互。 I have a model bar with a single string attribute of test.我有一个带有 test 单个字符串属性的模型栏。 I'm trying to loop through a series of strings which are the values for the test attribute and send POST request to my API.我正在尝试遍历一系列字符串,这些字符串是测试属性的值,并将 POST 请求发送到我的 API。

Here is the code for my Go client:这是我的 Go 客户端的代码:

for _,data := range attributes{
    client := new(http.Client)

    body := []byte(fmt.Sprintf("bar: {test: %s}", data))
    fmt.Println(string(body))

    req, err := http.NewRequest(
      "POST",
      "http://localhost:3000/bars.json",
      bytes.NewReader(body),
    )

On the backend of my Rails server here is the error that I am getting:在我的 Rails 服务器的后端,这是我得到的错误:

ActionController::ParameterMissing (param is missing or the value is empty: bar):
  
app/controllers/bars_controller.rb:70:in `bar_params'
app/controllers/bars_controller.rb:25:in `create'
Invalid or incomplete POST params

I've tried formatting my Go request a couple different ways but nothing seems to work properly with the API.我尝试用几种不同的方式格式化我的 Go 请求,但似乎没有任何东西可以与 API 一起正常工作。 How do I format the data for my post request correctly?如何正确格式化我的发布请求的数据?

  1. That's "bar: {test: %s}" not a valid json, try "{ "bar": {"test": "%s"} }" (notice quotes and brackets), its better to use json.Marshal .那是"bar: {test: %s}"不是一个有效的 json,尝试"{ "bar": {"test": "%s"} }" (注意引号和括号),最好使用json.Marshal
  2. Add json content type header req.Header.Set("Content-Type", "application/json; charset=UTF-8")添加 json 内容类型头req.Header.Set("Content-Type", "application/json; charset=UTF-8")

"bar: {test: %s}" is a random, arbitrary encoding that you've just invented, and Rails cannot possibly understand how to parse it, unless you've also written some custom decoder on the Rails side. "bar: {test: %s}"是您刚刚发明的随机、任意编码,Rails 不可能理解如何解析它,除非您还在 Rails 端编写了一些自定义解码器。

You can't invent new encodings of data on only one end of a communications channel.您不能仅在通信通道的一端发明新的数据编码。 You need to stick to encodings that both the client and the server can understand.您需要坚持客户端和服务器都可以理解的编码。 For HTTP, this typically means encoding your request body in a known format , for example application/x-www-form-urlencoded .对于 HTTP,这通常意味着以 已知格式对您的请求正文进行编码,例如application/x-www-form-urlencoded

Because this is part of a well-known standard, Go provides ways for you to do this easily;因为这是众所周知的标准的一部分,所以 Go 为您提供了轻松执行此操作的方法; it will handle encoding the data into the body of the request and setting the correct Content-Type header which tells Rails how to decode the body:它将处理将数据编码到请求正文中并设置正确的Content-Type标头,告诉 Rails 如何解码正文:

data := url.Values{
    "bar[test]": "%s"       
}

resp, err := http.PostForm("http://localhost:3000/bars.json", data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM