普通网友 2014-12-02 18:50
浏览 258
已采纳

收到错误的响应-在GO中发送POST请求时来自服务器的422

I am trying to send a POST request using this function - {

  func (Client *Client) doModify(method string, url string, createObj interface{}, respObject interface{}) error {

    bodyContent, err := json.Marshal(createObj)
    if err != nil {
        return err
    }

    client := Client.newHttpClient()

    req, err := http.NewRequest(method, url, bytes.NewBuffer(bodyContent))
    if err != nil {
        return err
    }

    Client.setupRequest(req)
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Content-Length", string(len(bodyContent)))

    resp, err := client.Do(req)

    if err != nil {
        return err
    }

    defer resp.Body.Close()

    if resp.StatusCode >= 300 {
        return errors.New(fmt.Sprintf("Bad response from [%s], go [%d]", url, resp.StatusCode))
    }

    byteContent, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return err
    }

    return json.Unmarshal(byteContent, respObject)
}

}

I am calling my function like this -

{

    func TestContainerCreate(t *testing.T) {
    client := newClient(t)
    container, err := client.Container.Create(&Container{
        Name:      "name",
        ImageUuid: "xyz",
    })

    if err != nil {
        t.Fatal(err)
    }

    defer client.Container.Delete(container)

}

}

The Create function calls internally calls the doCreate function which calls the doModify function pasted on the top . {

func (self *ContainerClient) Create(container *Container) (*Container, error) {
    resp := &Container{}
    err := self.Client.doCreate(container_TYPE, container, resp)
    return resp, err
}

}

{

   func (Client *Client) doCreate(schemaType string, createObj interface{}, respObject interface{}) error {
        if createObj == nil {
            createObj = map[string]string{}
        }

        schema, ok := Client.Types[schemaType]
        if !ok {
            return errors.New("Unknown schema type [" + schemaType + "]")
        }

        return Client.doModify("POST", collectionUrl, createObj, respObject)
    }

}

This gives me a 422 bad response.On doing further research, When doing a CURL, with "name" and "imageUuid" first letter as small case, gives a 201 created status but when passing "Name" and "ImageUuid" first letter as capital gives 422 bad response. Could there be issue with the json struct defined for container, or case of these entities being defined or something else? {

curl -X POST -v -s http://localhost:8080/v1/containers -H 'Content-Type: application/json' -d '{"name" : "demo", "imageUuid" : "docker:nginx"}'   | python -m 'json.tool'

}

Container struct definition looks like this - {

type Container struct {
    Resource

    ImageId string `json:"ImageId,omitempty"`

    ImageUuid string `json:"ImageUuid,omitempty"`

    MemoryMb int `json:"MemoryMb,omitempty"`

    Name string `json:"Name,omitempty"`

}

type ContainerCollection struct {
    Collection
    Data []Container `json:"data,omitempty"`
}

}

  • 写回答

1条回答 默认 最新

  • dongnachuang6635 2014-12-02 19:32
    关注

    string(len(bodyContent)) isn't doing what you think it is. You're converting a single int to a utf-8 string. You want to use the strconv package to get the numerical representation.

    Also note that you can't omitempty an int, since 0 is a valid value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法