2

I am trying to send a request to Identity verification company. They are asking to send three headers. I added the headers like so:

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("https://api.test");
                client.DefaultRequestHeaders.Accept.Add(new 
                MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
                client.DefaultRequestHeaders.TryAddWithoutValidation("hmac-signature", Hmackey);
                HttpRequestMessage request = new HttpRequestMessage();
                request.Method = HttpMethod.Post;

                request.Content = new StringContent(JSONFile, Encoding.UTF8,
                                    "application/json");
                HttpResponseMessage response = client.SendAsync(request).Result;
                string responseContent = await response.Content.ReadAsStringAsync();

I posted the same JSON file in SOAP UI and I got the response back, but when I send the same JSON file using my code above, I get below error:

{
    "responseHeader": {
        "tenantID": "V2321",
        "requestType": "PreciseIdOnly",
        "clientReferenceId": "PIDInitial-2312313",
        "expRequestId": "",
        "messageTime": "2020-06-09T17:17:49Z",
        "responseCode": "R0302",
        "responseType": "ERROR",
        "responseMessage": "The HMAC validation failed : Invalid Signature or no matching alias found."
    },
    "originalRequestData": null
}

The only difference is that in my code, I have "/r/n" and "/". I can remove "/r/n", but cannot remove "/" because double codes are preceded by forward slash.

Instead of calculating HMAC key, I got the HMAC key from SOAP UI from HTTP logs and put that in my code . I still get the same error saying "HMAC validation failed". I just want to make sure that does my code above looks right? I am supposed to add three headers like so:

Accept:application/json
Content-Type:application/json
hmac-signature:<TheCalculatedHMACSignature>

Does my code look right with these three headers. This is my first application where I am sending a request and I am thinking may be something in my code that is causing this error. I am not calculating HMAC key anymore. I am getting that key from SOAP UI Http logs and my response from SOAP UI looks good.

Any help in validating my code will be highly appreciated.

Anjali
  • 2,540
  • 7
  • 37
  • 77
  • `double codes are preceded by forward slash.` what does this mean? – mangusta Jun 10 '20 at 02:39
  • my partial JSOn looks like so: {\r\n \"header\": {\r\n \"tenantId\": \"V12345\",\r\n \"requestType\": \"PreciseIdOnly\",\r\n \"clientReferenceId\": \"PIDInitial-12345\" If you see tenantID, it is precedded by back slash – Anjali Jun 10 '20 at 02:46
  • why do you need those slashes? is the json string hardcoded somewhere in the code? – mangusta Jun 10 '20 at 02:51
  • I think, C# puts the slashes when there are quotes. I am generating the jSON file using classes and the resulting JSON has slashes and "/r/n" in it. I can rplace "/r/n" with empty string, but slashes are not going away. I think double quotes are escape characters in C# – Anjali Jun 10 '20 at 03:09
  • probably this is what you need: https://stackoverflow.com/questions/16692371/replacing-escape-characters-from-json – mangusta Jun 10 '20 at 03:28
  • I did this: JSONFile = Regex.Unescape(JSONFile); I still have forward slash in my JSONFile. I think there is no way to get rid of forward slash. C# puts them automatically in a string that has double quotes in it – Anjali Jun 10 '20 at 03:33
  • Does this answer your question? [send a Post HTTPRequest to a Identity Check company](https://stackoverflow.com/questions/62272323/send-a-post-httprequest-to-a-identity-check-company) – SiKing Jun 10 '20 at 22:35
  • No, My URL is fine – Anjali Jun 10 '20 at 23:09

0 Answers0