0

Mollie Payment fail while sending request in https://api.mollie.com/v2/payments

using HttpClient class in System.Net.Http

Mollie payment request using v2/payments API working fine through Postman but getting unauthorized request error in c# code

below code

HttpClient _httpClient = new HttpClient();

public HttpRequestMessage CreateHttpRequest(HttpMethod method, string relativeUri, HttpContent content = null)
{
    HttpRequestMessage httpRequest = new HttpRequestMessage(method, new Uri(new Uri(ApiEndPoint), relativeUri));
    httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this._apiKey);
    httpRequest.Content = content;

    return httpRequest;
}

public HttpResponseMessage SendHttpRequestAsync(HttpMethod httpMethod, string relativeUri, object data = null)
{
    HttpRequestMessage httpRequest = this.CreateHttpRequest(httpMethod, relativeUri);
    if (data != null)
    {       
        var content = new StringContent(new JavaScriptSerializer().Serialize(data), Encoding.UTF8, "application/json");
        httpRequest.Content = content;
    }

    HttpResponseMessage response = _httpClient.GetAsync(httpRequest.RequestUri.AbsoluteUri).Result;
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(response);
    if (response.IsSuccessStatusCode)
    {
        //here main logic
    }

    return response;
}

Message from received by Mollie server Mollie message

{
  "Version": {
    "Major": 1,
    "Minor": 1,
    "Build": -1,
    "Revision": -1,
    "MajorRevision": -1,
    "MinorRevision": -1
  },
  "Content": {
    "Headers": [
      {
        "Key": "Content-Length",
        "Value": [
          "209"
        ]
      },
      {
        "Key": "Content-Type",
        "Value": [
          "application/hal+json"
        ]
      }
    ]
  },
  "StatusCode": 401,
  "ReasonPhrase": "Unauthorized Request",
  "Headers": [
    {
      "Key": "X-Content-Type-Options",
      "Value": [
        "nosniff"
      ]
    },
    {
      "Key": "Strict-Transport-Security",
      "Value": [
        "max-age=31536000; includeSubDomains; preload"
      ]
    },
    {
      "Key": "Date",
      "Value": [
        "Tue, 21 Jan 2020 06:11:41 GMT"
      ]
    },
    {
      "Key": "Server",
      "Value": [
        "nginx"
      ]
    },
    {
      "Key": "WWW-Authenticate",
      "Value": [
        "Basic realm=\"Mollie API Key\""
      ]
    }
  ],
  "RequestMessage": {
    "Version": {
      "Major": 1,
      "Minor": 1,
      "Build": -1,
      "Revision": -1,
      "MajorRevision": -1,
      "MinorRevision": -1
    },
    "Content": null,
    "Method": {
      "Method": "GET"
    },
    "RequestUri": "https://api.mollie.com/v2/payments",
    "Headers": [],
    "Properties": {}
  },
  "IsSuccessStatusCode": false
}

But main issues is Test_api working in postman when hit the URL

Postman image

jishan siddique
  • 1,848
  • 2
  • 12
  • 23

3 Answers3

0

Try this way it will work :

using(var httpClient = new HttpClient()) {
 httpClient.SetBearerToken(tokenValue);
 var serData = JsonConvert.SerializeObject(modelData);
 var httpRequestContentData = new StringContent(serData, Encoding.UTF8, "application/json");
 using(var result = httpClient.PostAsync(url, httpRequestContentData).Result) {
  if ((int) result.StatusCode == (int) HttpStatusCode.OK) {
   // Go Ahead 
  }

 }
}
Nikunj Patel
  • 249
  • 3
  • 13
  • Thank you Nikunj but received this message using above code `StatusCode: 401, ReasonPhrase: 'Unauthorized Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { X-Content-Type-Options: nosniff Strict-Transport-Security: max-age=31536000; includeSubDomains; preload Date: Tue, 21 Jan 2020 08:53:12 GMT Server: nginx WWW-Authenticate: Basic realm="Mollie API Key" Content-Length: 209 Content-Type: application/hal+json ` – jishan siddique Jan 21 '20 at 08:54
  • If I'm adding token in the header this message received. `StatusCode: 422, ReasonPhrase: 'Unprocessable Entity', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { X-Content-Type-Options: nosniff Strict-Transport-Security: max-age=31536000; includeSubDomains; preload Date: Tue, 21 Jan 2020 09:00:17 GMT Server: nginx Content-Length: 213 Content-Type: application/hal+json }` – jishan siddique Jan 21 '20 at 09:02
0

Regarding your case, you should add the Bearer on the header for the _httpclient:

_httpClient.DefaultRequestHeaders.Authorization =new AuthenticationHeaderValue("Bearer", this._apiKey);

Now, since you are sending a request body to a GET request which is somewhat unconventional construct that falls in a gray area of the HTTP specification - the end result is that many older pieces of software either cannot handle such a request at all, or will explicitly reject it because they believe it to be malformed.

.NET Framework doesn't support this out-of-the-box (you will receive a ProtocolViolationException). You can install System.Net.Http.WinHttpHandler and use it instead of the default HttpClientHandler when constructing your client:

var handler = new WinHttpHandler();
HttpClient _httpClient = new HttpClient(handler);
//Same code as in question
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
-1

Calling _httpClient.GetAsync(Uri) creates a brand-new HttpRequestMessage object internally, instead of using your object configured with an Authorization: header.

Use _httpClient.SendAsync(httpRequest) instead to actually use your object.

yaakov
  • 5,552
  • 35
  • 48