Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Type with charset causes failed pipeline #951

Closed
jrtitus opened this issue Sep 17, 2021 · 4 comments · Fixed by #952
Closed

Content-Type with charset causes failed pipeline #951

jrtitus opened this issue Sep 17, 2021 · 4 comments · Fixed by #952
Assignees
Labels
2-medium priority denoting issues with cross-cutting project impact enhancement New feature or request
Milestone

Comments

@jrtitus
Copy link
Contributor

jrtitus commented Sep 17, 2021

Good request:

$ http :59770/api/v2/trigger Content-Type:application/json;charset=utf-8 action=set deviceName=SomeDevice commandName=SomeCommand resourceName=SomeResource value=42
HTTP/1.1 200 OK
Content-Length: 36
Content-Type: application/json
Date: Thu, 16 Sep 2021 22:06:19 GMT

{
    "apiVersion": "v2",
    "statusCode": 200
}

Bad request:

$ http :59770/api/v2/trigger Content-Type:"application/json;charset=utf-8" action=set deviceName=SomeDevice commandName=SomeCommand resourceName=SomeResource value=42
HTTP/1.1 400 Bad Request
Content-Length: 155
Content-Type: text/plain; charset=utf-8
Date: Thu, 16 Sep 2021 21:59:48 GMT

unable to process custom object received of type 'send-command/functions.ActionRequest': unsupported content-type 'application/json;charset=utf-8' recieved

Error message from the application service:

level=ERROR ts=2021-09-16T23:02:21.4004789Z app=app-send-command source=runtime.go:308 msg="unable to process custom object received of type 'send-command/functions.ActionRequest': unsupported content-type 'application/json;charset=utf-8' recieved. X-Correlation-ID="

The previous example was done using HTTPie, but I initially found this was an issue when attempting to make this same request from a .NET core application using

private static readonly HttpClient client = new HttpClient();
// ...
client.BaseAddress = new Uri($"http://{host}:{port}");
var content = new StringContent(
	JsonConvert.SerializeObject(request),
	Encoding.UTF8,
	"application/json"
);
client.PostAsync("api/v2/trigger", content);

I think the issue is coming from this switch statement, or the envelope.ContentType isn't being converted well.

switch envelope.ContentType {
case common.ContentTypeJSON:
err = json.Unmarshal(envelope.Payload, target)
case common.ContentTypeCBOR:
err = cbor.Unmarshal(envelope.Payload, target)
default:
err = fmt.Errorf("unsupported content-type '%s' recieved", envelope.ContentType)
}

@lenny-goodell
Copy link
Member

It is the addition of the ;charset=utf-8 on the end that is causing the error. application/json;charset=utf-8 is not currently supported.

@jrtitus
Copy link
Contributor Author

jrtitus commented Sep 17, 2021

Wow, I thought I had tried that and came up with the same result, but I now see what is happening. When I make the request with HTTPie, it treats the ; character as a separator and sets "charset":"utf-8" in the POST body instead of as part of the header. With the quotes, it makes sure to keep it as part of the Content-Type header, so my initial thought was skewed by that.

I confirmed that with quotes, and without ;charset=utf-8 works, so it's not the quotes. I'll update the title of the issue to reflect that.

$ http :59770/api/v2/trigger Content-Type:"application/json" action=set deviceName=SomeDevice commandName=SomeCommand resourceName=SomeResource value=42

HTTP/1.1 200 OK
Content-Length: 36
Content-Type: application/json
Date: Fri, 17 Sep 2021 16:25:52 GMT

{
    "apiVersion": "v2",
    "statusCode": 200
}

@jrtitus jrtitus changed the title Content-Type with quotes causes failed pipeline Content-Type with charset causes failed pipeline Sep 17, 2021
@jrtitus
Copy link
Contributor Author

jrtitus commented Sep 17, 2021

@lenny-intel Do you think the solution to this might simply be to split envelope.ContentType on the ; character in order to make an accurate comparison? json.Unmarshal doesn't really care what the charset is, right?

@lenny-goodell
Copy link
Member

Yes, split and ignore all but the first item.

lenny-goodell pushed a commit that referenced this issue Sep 17, 2021
…parison (#951) (#952)

Signed-off-by: Jeff Titus <jeff.rtitus@gmail.com>
@lenny-goodell lenny-goodell added this to the Jakarta milestone Sep 17, 2021
@lenny-goodell lenny-goodell added 2-medium priority denoting issues with cross-cutting project impact enhancement New feature or request labels Sep 17, 2021
FelixTing pushed a commit to FelixTing/app-functions-sdk-go that referenced this issue Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2-medium priority denoting issues with cross-cutting project impact enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants