1

I'm trying to integrate with a payment gateway (PagSeguro), according to the documentation, I must use the Accept Header "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1".

Trying with the code not work:

HttpClient.DefaultRequestHeaders.Clear();
HttpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1");
var content = new StringContent(json, Encoding.Default, "application/json");
var response = await HttpClient.PostAsync(enderecoPreApprovals, content);
var responsestr = await response.Content.ReadAsStringAsync();

I also tried using the same code from documentation:

var client = new RestClient(url) {Timeout = -1};
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1");
request.AddParameter("application/json", content, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

and using flurl:

var response = await addresPreApprovals
                    .WithHeader("Accept", "application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1")
                    .WithHeader("Content-Type", "application/json")
                    .PostJsonAsync(adesaoDto);

All the responses are Accept header field is mandatory.. It's like Accept Header is not recognized.

The problem is not in API why i tried using Postman and Insomnia and it's works perfectly.

1 Answers1

0

The problem occurs because of the space between the semicolon (;) and the word charset. We were unable to make it work in .NET 3.1 and there is even an issue on github, indicating that it is not a problem, an implementation of the RFC 7231 specification. Whenever a string "var1 = val1; var2 = val2", o is indicated. NET converts to "var1 = val1; var2 = val2". However, in .NET 5.0, an implementation was released that allows entering a raw string. Another option, according to an old post, is to create something with .NET Framework 4.5

In .NET 5.0

    var acceptValue = "application/vnd.pagseguro.com.br.v1+json;charset=ISO-8859-1";
    var requestMessage = new HttpRequestMessage(HttpMethod.Post, urlApprovals);
    requestMessage.Headers.TryAddWithoutValidation("accept", acceptValue);

GitHub ref: https://github.com/dotnet/runtime/issues/30171

User who solved the problem using .NET Framework 4.5: https://stackoverflow.com/a/40162071/2112736

Gilberto Alexandre
  • 2,227
  • 1
  • 18
  • 20