Flurl: Why can't the content language be set for a POST request using JSON?

Created on 17 Dec 2018  路  1Comment  路  Source: tmenier/Flurl

Issue

I would like to POST a JSON request with the content header Content-Language specified. I already managed to set the content type (Content-Type) without any issues:

```c#
string response = await "https://jsonplaceholder.typicode.com/".AppendPathSegment("posts")
.WithHeaders(new { Content_Type = "application/json; charset=UTF-8" })
.PostJsonAsync(new { title = "bar", body = "foo", userId = 1 }).ReceiveString();

Console.WriteLine(response);

correctly returning:
```json
{
  "title": "bar",
  "body": "foo",
  "userId": 1,
  "id": 102
}

However, trying to specify Content-Language in addition:
```c#
string response = await "https://jsonplaceholder.typicode.com/".AppendPathSegment("posts")
.WithHeaders(new { Content_Type = "application/json; charset=UTF-8", Content_Language = "en-US" })
.PostJsonAsync(new { title = "bar", body = "foo", userId = 1 }).ReceiveString();

Console.WriteLine(response);

breaks the code giving the error

> Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

I understand that content headers should be set on the content and not the request, however, after reading [answer](https://stackoverflow.com/a/46369967/7061914) (and [this one](https://stackoverflow.com/a/46920875/7061914)) it seemed like there's no need to distinct request and content levels anymore with Flurl 2.0+.

## Side note
The used API is a mock. I'm aware that using `PostJsonAsync` automatically sets the `Content-Type` header, however, for my use case, it will be set to `application/vnd.api+json` and therefore, needs to be specified explicitly.

# Question
**What am I doing wrong? Is there anything I do not understand about content headers?
What makes `Content-Language` any different from `Content-Type` when adding it to a request?**

# Update
Is this issue possibly related to the fact that `Content-Language` is not a case in [`SetHeader`](https://github.com/tmenier/Flurl/blob/dev/src/Flurl.Http/HttpMessage.cs#L41), thus setting `Content-Language` as request level header which then leads to the error posted above?

Actually this assumption seems to be correct, adding a case for `SetHeader` prevents the above error from occurring after recompiling Flurl:

```c#
public void SetHeader(string name, object value, bool createContentIfNecessary) {
  switch (name.ToLower()) {
    // https://msdn.microsoft.com/en-us/library/system.net.http.headers.httpcontentheaders.aspx
    case "content-disposition":
    case "content-language": // Add this to successfully set `Content-Language`.
    case "content-length":
    case "content-location":
    case "content-md5":
    case "content-range":
    case "content-type":
    case "expires":
    case "last-modified":
        // it's a content-level header
    if (Content == null && (!createContentIfNecessary || value == null))
        break;

    if (Content == null) {
        Content = new CapturedStringContent("");
        Content.Headers.Clear();
    }
    else {
        Content.Headers.Remove(name);
    }

    if (value != null)
        Content.Headers.TryAddWithoutValidation(name, new[] { value.ToInvariantString() });
    break;
    default:
    // it's a request-level header
    Headers.Remove(name);
    if (value != null)
        Headers.TryAddWithoutValidation(name, new[] { value.ToInvariantString() });
    break;
    }
}

Why is Content-Language not added as a case here?

bug

Most helpful comment

Thanks for reporting. It looks like I just missed a few. All of these should be accounted for:

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.headers.httpcontentheaders

I'll get it fixed for the next release.

>All comments

Thanks for reporting. It looks like I just missed a few. All of these should be accounted for:

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.headers.httpcontentheaders

I'll get it fixed for the next release.

Was this page helpful?
0 / 5 - 0 ratings