普通网友 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.

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

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法