edgexfoundry / edgexfoundry/device-sdk-go
Handle both JSON request bodies as well as CBOR request bodies.
- Dominant language
- Go
- Stars
- 106
- Forks
- 136
- Avg merge
- 1h 36m
- Merged PRs (30d)
- 1
Description
- Currently, we do not check the header value (i.e. whether `ContentType` is JSON or CBOR). Creating this functionality will be helpful and can be used in the future when such functionality possibly becomes a requisite.
- This functionality has actually _already_ been largely addressed, as part of [some CBOR-related work done](https://github.com/edgexfoundry/edgex-go/issues/2272) in the `edgex-go` repo, so there is no need to start from scratch.
- More details can be found on the following feature branch: [Functionality largely addressed](https://github.com/akramtexas/device-sdk-go/tree/fix_device_sdk_go_response_cbor_issue_2272).
- Mentioning the following snippet for reference:
```
func parseParams(params string, r *http.Request) (paramMap map[string]string, err error) {
if r == nil {
return nil, fmt.Errorf("error in request parameter that was passed in")
}
// Check the header value
switch r.Header.Get(clients.ContentType) {
case clients.ContentTypeCBOR:
err = codec.NewDecoderBytes([]byte(params), &codec.CborHandle{}).Decode(¶mMap)
case clients.ContentTypeJSON:
err = json.Unmarshal([]byte(params), ¶mMap)
default:
common.LoggingClient.Error(fmt.Sprintf("header value was neither JSON nor CBOR, instead was: %s", r.Header.Get(clients.ContentType)))
}
if err != nil {
common.LoggingClient.Error(fmt.Sprintf("parsing Write parameters failed %s, %v", params, err))
return
}
if len(paramMap) == 0 {
err = fmt.Errorf("no parameters specified")
return
}
return
}
```
Contributor guide
Assessment
This issue has not been assessed yet.